Share
 
 

SetFont Method (ActiveX)

Sets the definition data of the font for the TextStyle.

Supported platforms: Windows only

Signature

VBA:

object.SetFont Typeface, Bold, Italic, CharSet, PitchAndFamily
object

Type: TextStyle

The object this method applies to.

Typeface

Access: Input-only

Type: String

The typeface (font name).

Bold

Access: Input-only

Type: Boolean

Sets the TextStyle to bold.

  • True: The TextStyle is bold.
  • False: The TextStyle is not bold.
Italic

Access: Input-only

Type: Boolean

Sets the TextStyle to italic.

  • True: The TextStyle is italic.
  • False: The TextStyle is not italic.
CharSet

Access: Input-only

Type: Long

The character set for the font. (See the available values in the Remarks section.)

PitchAndFamily

Access: Input-only

Type: Long

The pitch and family definitions for the font. (See the available values in the Remarks section.)

Return Value (RetVal)

No return value.

Remarks

In some instances, the name of the font might not update correctly after changing the font file assigned to a text style. The most common situation in which this might occur is after a font substitution happens during the opening of a drawing file. It is recommended to change both the font file and font name (typeface) together using the FontFile property and SetFont method of an TextStyle object. See the About Assigning Fonts in the ActiveX Developer's Guide for more information on working with fonts.

The following section provides definitions for constants to use with this method. To use these constants, copy the definitions you need and paste them into the Declarations section of your application. A complete list of the constants that are available from Microsoft can be found in the file win32api.txt, which is provided with the Visual Basic Development Environment.

CharSet

The CharSet parameter specifies the character set for the font. To use the following constants in your VB or VBA application, copy the definitions into the Declaration section of your code.

Public Const ANSI_CHARSET = 0
Public Const DEFAULT_CHARSET = 1
Public Const SYMBOL_CHARSET = 2
Public Const SHIFTJIS_CHARSET = 128
Public Const OEM_CHARSET = 255
PitchAndFamily

The PitchAndFamily parameter specifies the pitch and family values for the font. The value is determined by a combination of three different settings. To provide the PitchAndFamily value, choose a setting from each of the categories, and then use the OR operator to combine them. A setting is required from the first two categories; the pitch and the family. The third category, the TrueType Flag, is only used when you are specifying a TrueType font.

To use the following constants in your VB or VBA application, copy the definitions into the Declaration section of your code.

' Pitch Values
Public Const DEFAULT_PITCH = 0
Public Const FIXED_PITCH = 1
Public Const VARIABLE_PITCH = 2
' Family Values
Public Const FF_DONTCARE = 0    '  Don't care or don't know.
Public Const FF_ROMAN = 16      '  Variable stroke width, serifed.
Public Const FF_SWISS = 32      '  Variable stroke width, sans-serifed.
Public Const FF_MODERN = 48     '  Constant stroke width, serifed or sans-serifed.
Public Const FF_SCRIPT = 64     '  Cursive, etc.
Public Const FF_DECORATIVE = 80 '  Old English, etc.
' TrueType Flag
Public Const TMPF_TRUETYPE = &H4

Examples

VBA:

Sub Example_SetFont()
    ' This example finds the font information for the active text style.
    ' It then changes the font to bold.
    
    Dim typeFace As String
    Dim Bold As Boolean
    Dim Italic As Boolean
    Dim charSet As Long
    Dim PitchandFamily As Long
    
    ThisDrawing.ActiveTextStyle.GetFont typeFace, Bold, Italic, charSet, PitchandFamily
    
    MsgBox "The current text style has the following font properties:" & vbCrLf _
           & "Typeface: " & typeFace & vbCrLf _
           & "Bold: " & Bold & vbCrLf _
           & "Italic: " & Italic & vbCrLf _
           & "Character set: " & charSet & vbCrLf _
           & "Pitch and Family: " & PitchandFamily
            
    ' Change the bold property
    Bold = Not Bold
    
    ThisDrawing.ActiveTextStyle.SetFont typeFace, Bold, Italic, charSet, PitchandFamily
    MsgBox "The current text style has the following font properties:" & vbCrLf _
           & "Typeface: " & typeFace & vbCrLf _
           & "Bold: " & Bold & vbCrLf _
           & "Italic: " & Italic & vbCrLf _
           & "Character set: " & charSet & vbCrLf _
           & "Pitch and Family: " & PitchandFamily
            
    ' Reset the font
    Bold = Not Bold
    ThisDrawing.ActiveTextStyle.SetFont typeFace, Bold, Italic, charSet, PitchandFamily

End Sub

Visual LISP:

(vl-load-com)
(defun c:Example_SetFont()
    ;; This example finds the font information for the active text style.
    ;; It then changes the font to bold.
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))

    (vla-GetFont (vla-get-ActiveTextStyle doc) 'typeFace 'Bold 'Italic 'charSet 'PitchandFamily)
    
    (alert (strcat "The current text style has the following font properties:"
                   "\nTypeface: " typeFace
                   "\nBold: " (if (= Bold :vlax-true) "True" "False")
                   "\nItalic: " (if (= Italic :vlax-true) "True" "False")
                   "\nCharacter set: " (itoa charSet)
                   "\nPitch and Family: " (itoa PitchandFamily)))
            
    ;; Change the bold property
    (setq Bold (if (= Bold :vlax-true) :vlax-false :vlax-true))

    (vla-SetFont (vla-get-ActiveTextStyle doc) typeFace Bold Italic charSet PitchandFamily)
  
    (alert (strcat "The current text style has the following font properties:"
                   "\nTypeface: " typeFace
                   "\nBold: " (if (= Bold :vlax-true) "True" "False")
                   "\nItalic: " (if (= Italic :vlax-true) "True" "False")
                   "\nCharacter set: " (itoa charSet)
                   "\nPitch and Family: " (itoa PitchandFamily)))
            
    ;; Reset the font
    (setq Bold (if (= Bold :vlax-true) :vlax-false :vlax-true))
    (vla-SetFont (vla-get-ActiveTextStyle doc) typeFace Bold Italic charSet PitchandFamily)
)

Was this information helpful?