Italics in Formatting Illustrator from Excel Spreadsheet using VBA

714 Views Asked by At

I'm creating a series of tags in Illustrator, using VBA in excel (the excel worksheet has the information that populates the tags), and I cannot find a way to specify that the font which appears in Illustrator is italicized and a particular font.

Using:

.TextRange.CharacterAttributes.TextFont = appIll.TextFonts.Item("Arial") 

lends the same result as using:

.TextRange.CharacterAttributes.TextFont = appIll.TextFonts.Item("Monotype Corsiva")

And needless to say, I also can't get italics. I'm very new to this, but would appreciate anyone letting me know how to specify the font and font-style. Thanks!

            .TextRange.ParagraphAttributes.Justification = aiCenter
            .TextRange.CharacterAttributes.size = dblTopLine1FontSize
            .TextRange.CharacterAttributes.StrokeWeight = 0.35
            .TextRange.CharacterAttributes.StrokeColor = clrStrokeColor
            .TextRange.CharacterAttributes.FillColor = clrFontFillColor
            SetItalics tfrmTopLine1
            .CreateOutline
        End With
1

There are 1 best solutions below

8
On BEST ANSWER

have a look at the following to see if it helps:

Firstly, at the risk of stating the obvious, I first identified that the font I 'needed' to use was indeed accessible to my copy of Illustrator - as it happens, to use Monotype Corsiva in code it has to be "MonotypeCorsiva"! The lessons here are that the 'real' font name may be different from the Illustrator displayed font name and the 'real' font name also indicates the 'style'. I used the following code which simply listed the font and its 'style' to Excel's Immediate Window. Illustrator needs to be open for this example.

Sub IllFnts()
Dim IApp As New Illustrator.Application
Set IApp = GetObject(, "Illustrator.Application")

Dim fnt As Illustrator.TextFont

    For Each fnt In IApp.TextFonts
        Debug.Print fnt.Name & "  -  " & fnt.Style
    Next
End Sub

I then added a Point Text Frame, added some text and changed the TextFont with the code below:

EDIT - UPDATE TO INCLUDE A MEANS OF APPLYING ITALIC

Sub TestChngeFnt()
Dim IApp As New Illustrator.Application

    If IApp Is Nothing Then
        Set IApp = CreateObject("Illustrator.Application")
    Else
        Set IApp = GetObject(, "Illustrator.Application")
    End If

Dim fnt As Illustrator.TextFont

'A distinctive font for reference?
Set fnt = IApp.TextFonts("Algerian")

'Add a Document
Set docRef = IApp.Documents.Add()

'Add some Point Text
Set pointTextRef = docRef.TextFrames.Add()
pointTextRef.Contents = "Some Text in a Point TextFrame"
pointTextRef.Top = 700
pointTextRef.Left = 20
pointTextRef.Selected = True
pointTextRef.TextRange.CharacterAttributes.Size = 35
IApp.Redraw

'Set distinctive font
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)

MsgBox "Have a look at the text font before changing to another."

'set a new font to 'regular'
Set fnt = IApp.TextFonts("BodoniMT")
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)

MsgBox "New text font before changing to italics."

'set font to 'italics' within the same font family?
Set fnt = IApp.TextFonts("BodoniMT-Italic")
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)


End Sub

This example includes a couple of message boxes to pause the code to observe the text changes. Applying 'italic' in this example is really selecting a font from the same font family designed as italic? Not completely sure if this is the 'correct' approach with Illustrator or just a workaround,but it may take you forward a little.

You may also find this Adobe Illustrator CS5 Scripting Reference: vbScript useful although the Object Browser in Excel is also a good starting reference.