TextStyles control the selection of font. The object structure is similar to Layers (and Blocks, DimStyles, and several other collections whose important methods are Add, Item and property Count). After the basic skeleton the methods and properties differ.

Dim objtxtstyles As AcadTextStyles
Dim objtxtstyle As AcadTextStyle
Set objtxtstyles = acadDoc.TextStyles
‘Set objtxtstyle = objtxtstyles.Item(“ABC”) ‘error if does not exist
‘Set objtxtstyle = objtxtstyles.Item(1)
Set objtxtstyle = objtxtstyles.Add(“ABC”) ‘returns reference if already exists

The new textstyle needs to be current for new entities to adopt its property. The ActiveTextStyle property of the drawing object is used.

acadDoc.ActiveTextStyle = acadDoc.TextStyles.Item(“ABC”)
acadDoc.ActiveTextStyle = objtxtstyle

Application – Documents – Document – TextStyles – TextStyle

After the textstyle is added, the method SetFont is used to select font. It loads the font typeface into a string variable, Booleans for Bold and Italic, and two fields called CharacterSet and PitchandFamily. I am ignoring those last two and using constants obtained from GetFont which is SetFont in reverse. The help has a full explanation. The sample code for SetFont and GetFont can be pasted in whole and is very useful for decoding.

Dim typeFace As String
Dim Bold As Boolean, Italic As Boolean
Dim charSet As Long, PitchandFamily As Long

typeFace = “Arial”
Bold = False
Italic = False
charSet = 0
PitchandFamily = 34
objtxtstyle.SetFont typeFace, Bold, Italic, charSet, PitchandFamily


After SetFont is run, the the truetype textstyle is complete. The properties can be tweaked, if needed.

Autocad has truetype fonts, such as Arial and Calibri, and an old style shape font with .shx extension, the classic RomanS and before that Simplex, which many drafters still use. To create an SHX font, the typeface is left an empty string in SetFont, and the property Fonfile is set.

The TextStyles collection contains all the TextStyle objects in the drawing.
TextStyles collection – AcadTextStyles
Methods – Add, Item
Properties – Count

TextStyle object – AcadTextStyle
Method – Delete, GetFont, SetFont
Properties – FontFile, Height, LastHeight, Name, ObliqueAngle, Width

Sub arial()
Call new_textstyle("Arial", "Arial", False, False)
End Sub

Sub arial_n()
Call new_textstyle("Arial Narrow", "Arial Narrow", False, False)
End Sub

Sub calibri()
Call new_textstyle("Calibri", "Calibri", False, False)
End Sub

Sub calibri_l()
Call new_textstyle("Calibri Light", "Calibri Light", False, False)
End Sub

Sub courier_n()
Call new_textstyle("Courier New", "Courier New", False, False)
End Sub

Sub romans()
Call new_textstyle("RomanS", "", False, False)

Dim objtxtstyle As AcadTextStyle
Set objtxtstyle = acadDoc.ActiveTextStyle
objtxtstyle.fontFile = "romans.shx"
objtxtstyle.width = 0.75
End Sub

Sub new_textstyle(strname As String, typeface As String, bold As Boolean, italic As Boolean)
Call connect_acad

Dim objtxtstyles As AcadTextStyles
Dim objtxtstyle As AcadTextStyle
Set objtxtstyles = acadDoc.TextStyles

Set objtxtstyle = objtxtstyles.Add(strname) 'returns reference if already exists

acadDoc.ActiveTextStyle = objtxtstyle

'default values of a new textstyle
'objtxtstyle.fontFile = ""
'objtxtstyle.height = 0
'objtxtstyle.LastHeight= 0.2
'objtxtstyle.Name = "ABC"
    Dim charSet As Long
    Dim PitchandFamily As Long
    charSet = 0
    PitchandFamily = 34

objtxtstyle.SetFont typeface, bold, italic, charSet, PitchandFamily

End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s