Change textstyle Autocad in vba

Change textstyle Autocad in vba

Anonymous
Not applicable
8,426 Views
4 Replies
Message 1 of 5

Change textstyle Autocad in vba

Anonymous
Not applicable

Hello, 

 

When i insert my text ist in a standardstyle. but my text needs to be in a specific style "Border". But i don't know how to do this.

 

I have:

 

Set Mtextobj= thisdrawing.Textstyles="Border"

 

But this doesn't work

 

I am still a starter with the vba program, can somebody help me?

 

 

0 Likes
8,427 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

Norman gave you a good hint already in this post

 

go through AutoCAD ActiveX and VBA Reference example, too

you'll find most of what you need

here's what you'll find as "TextStyle example

Sub Example_TextStyle()
   ' This example creates an aligned dimension in model space and
   ' creates a new system text style.  The new text style is then assigned to
   ' the new dimension

    Dim dimObj As AcadDimAligned
    Dim newText As AcadTextStyle
    Dim point1(0 To 2) As Double, point2(0 To 2) As Double
    Dim location(0 To 2) As Double
    
    ' Define the dimension
    point1(0) = 5: point1(1) = 5: point1(2) = 0
    point2(0) = 5.5: point2(1) = 5: point2(2) = 0
    location(0) = 5: location(1) = 7: location(2) = 0
    
    ' Create an aligned dimension object in model space
    Set dimObj = ThisDrawing.ModelSpace.AddDimAligned(point1, point2, location)    
    ' Create new text style
    Set newText = ThisDrawing.TextStyles.Add("MYSTYLE")
    newText.height = 0.5    ' Just set the height of the new style so we can differentiate
    
    ThisDrawing.Application.ZoomAll
    
    ' Read and display the current text style for this dimension
    MsgBox "The text style is currently set to: " & dimObj.textStyle
    
    ' Change the text style to use the new style we created
    dimObj.textStyle = "MYSTYLE"    ThisDrawing.Regen acAllViewports
    
    ' Read and display the current text style for this dimension
    MsgBox "The text style is now set to: " & dimObj.textStyle

End Sub

 so that you must first get the your MText object and then set its "textStyle" property (see bold text above)

 

0 Likes
Message 3 of 5

kasperwuyts
Collaborator
Collaborator

I think you need to get some background VBA information on what objects, properties, methods, collections, etc... are and how to use them. That way, the Autocad object model will seem a lot more intuitive.


For example, to change the text style of a specific text object, you use its 'Textstyle' property, which simply contains a string with the name of the text style.

To change it, you would for example use:

MyText.TextStyle = "Border"

(assuming that you have a text object called 'MyText' and a text style called 'Border')

 

This is not to be confused with the 'TextStyles' property or the 'AcadTextStyle' object

 

Textstyles is a property of a drawing object. It is a collection, containing a bunch of 'AcadTextStyle' objects. You would use it if you actually want to edit the text styles themselves, or create and delete them.

 

For example, the following routine gives you a list of all the textstyles in your drawing:

 

Sub test456()

    Dim Textstyleslist As String
    
    For n = 0 To ThisDrawing.TextStyles.Count - 1
    Textstyleslist = Textstyleslist + vbNewLine + ThisDrawing.TextStyles.Item(n).Name
    Next
    
    MsgBox Textstyleslist
    
End Sub

 


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

Anonymous
Not applicable

How the change a textcolor? 

 

I sended a picture ... i got a name and a number next to it. the number needs to be in red.. how can i change that? 

 

Thanks? 🙂

0 Likes
Message 5 of 5

kasperwuyts
Collaborator
Collaborator

Overriding the formatting of specific text needs to be done by using special formatting syntax in the string itself. Knowing all that syntax is a whole discipline in itself; but there is a little trick you can use. Create the multitext object in Autocad without VBA, and go to properties. Under 'contents' you will see the actual syntax that is needed to format the text. See the jpeg i uploaded.

 

It looks like this:

'test {\C1;test} test'

 

So if you want to create a text object in VBA with this formatting; that needs to be the string value of your text.

Example:

 

Sub test512()
Dim mymtext As AcadMText
Dim MyMTextString As String
Dim Mypoint As Variant
Mypoint = ThisDrawing.Utility.GetPoint(, "specify location:")
MyMTextString = "test {\C1;test} test"
Set mymtext = ThisDrawing.ActiveLayout.Block.AddMText(Mypoint, 20, MyMTextString)
End Sub

 


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