Text in layout with vba

Text in layout with vba

Anonymous
Not applicable
1,151 Views
4 Replies
Message 1 of 5

Text in layout with vba

Anonymous
Not applicable

Hello,

 

I have a layout and i need to get some text in the layout, such as my name, street,... 

But i don't know how i need to do this. How can i insert text ( multiple lines)

 

I hope somebody can help me,

Thanks

0 Likes
1,152 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

here's the AutoCAD ActiveX and VBA Reference example for inserting a MText that you can adapt to your needs

Sub Example_AddMtext()
    ' This example creates an MText object in model space.
    
    Dim MTextObj As AcadMText
    Dim corner(0 To 2) As Double
    Dim width As Double
    Dim text As String
    corner(0) = 0#: corner(1) = 10#: corner(2) = 0#
    width = 10
    text = "This is the text String for the mtext Object"

    ' Creates the mtext Object
    Set MTextObj = ThisDrawing.ModelSpace.AddMText(corner, width, text)
    ZoomAll
    
End Sub

 

0 Likes
Message 3 of 5

Anonymous
Not applicable

how to put the tekst in a layoutpaper? now its in modelspace. but when i do thisdrawing.modelspace to thisdrawing.layouts it gives a error... 

 

0 Likes
Message 4 of 5

Anonymous
Not applicable

Imports Autodesk.AutoCAD.Interop.Common

Public Class Class1
Public AcadApp As Autodesk.AutoCAD.Interop.AcadApplication
Public AcadDoc As Autodesk.AutoCAD.Interop.AcadDocument

<Autodesk.AutoCAD.Runtime.CommandMethod("DanLinea")> Public Sub Inptext()
AcadApp = GetObject(, "Autocad.Application.20.1")
AcadDoc = AcadApp.ActiveDocument
'
Dim Pt1(2), Pt2(2) As Double
'
Pt1(0) = 0
Pt1(1) = 1
Pt1(2) = 0
'
Pt2(0) = 10
Pt2(1) = 0
Pt2(2) = 0
'
Dim ObjText1 As AcadText
Dim ObjText2 As AcadText
ObjText1 = AcadDoc.ModelSpace.AddText("ModelSpace", Pt1, 10) 'Inserts Text Into Model
ObjText2 = AcadDoc.PaperSpace.AddText("PaperSpace", Pt1, 10) 'Inserts Text Into Layout
AcadApp.Application.ZoomExtents()

End Sub
End Class

0 Likes
Message 5 of 5

kasperwuyts
Collaborator
Collaborator

Instead of 'thisdrawing.modelspace'

Use 'thisdrawing.paperspace'

This will add the text to the currently active layout PROVIDED a layout is active.

Another solution is to use:

'thisdrawing.activelayout.block'

-> Which will use the currently active sheet, whether is it modelspace or paperspace.

Another option is:

'thisdrawing.layouts(<index number>).block'

-> this will use a very specific layout, not per se the one active. Ofcourse you replace <index number> with the correct integer (depends on the layout).

 

Kasper


Best regards
Kasper Wuyts
_______________________________________________________________________________
If this post solves your problem, clicking the 'accept as solution' button would be greatly appreciated.
0 Likes