Skip to main content
CarlosCanto
Community Expert
Community Expert
March 17, 2012
Question

Illustrator VBA scripting 101 - via Excel

  • March 17, 2012
  • 8 replies
  • 38126 views

This post will attempt to introduce newcomers to Illustrator Visual Basic Scripting (well, not actually vbs, but rather tru VBA, Visual Basic for Applications). I personally prefer vba over bvs for a number of reasons. First, I always have Excel and Illustrator open, so it makes sense for me use it to drive Ai. Second, I usually need to transfer data between the two programs. Third...I love the Excel IDE...ok, let's get right into it.

- Open Excel

- hit Alt+F11, to bring up the editor

- in the Tools menu, click on References...

- add a reference to "Adobe Illustrator CS5 Type Library"

- in the Personal.xls (or in any other book) add a Module. Personal is a global workbook that is always available. If you don't see it, go back to Excel and record a macro, anything will do. That will create the Personal file.

- and type the following in that module

- we have to continue the tradition and do the "HelloWorld" script

Sub helloWorld()
    Dim iapp As New Illustrator.Application
    Dim idoc As Illustrator.Document
    Dim iframe As Illustrator.TextFrame
   
    Set idoc = iapp.ActiveDocument
    Set iframe = idoc.TextFrames.Add
   
    iframe.Contents = "Hello World from Excel VBA!!"
   
    Set iframe = Nothing
    Set idoc = Nothing
    Set iapp = Nothing
End Sub

- save Personal book

- open Illustrator and create a new document first

- to run, move the cursor anywhere inside the Sub...End Sub and hit F5

that's it for now...in the following posts we'll move the text to the middle of the page, create new documents, get data from an Excel Range to Illustrator, get data from Illustrator text frame to an Excel Range...and more, hopefully.

questions? comments?

This topic has been closed for replies.

8 replies

Participant
August 27, 2021

Sorry for bumping such an old post.

 

I have good vba skills and have created illustrator code before but never used the layers really. 

 

@CarlosCanto I am trying to get into the layers in illustrator. I can select the parent layer but how do i go deeper into the sublayers contained in that layer?

 

I've tried everything.

 

Thanks in advance

 

Euan

CarlosCanto
Community Expert
Community Expert
August 28, 2021

to access sublayers you just have to chain commands (using dot notation) 

 

for example, to get the width of the first item in the first sublayer of the first layer

msgBox Illustrator.Application.ActiveDocument.Layers(1).Layers(1).PageItems(1).width
Participant
March 1, 2019

hello, when I do all that i have an error which says "ActiveX component can't create object or return reference to this object (Error 429)"  and it brings me to that line "Set idoc = iapp.Documents.Add" . I'm not a programmer at all and using the last version of illustrator. Could you help me please ? CarlosCanto

Participant
October 17, 2015

How can i change a  frame text that already exists.

CarlosCanto
Community Expert
Community Expert
October 17, 2015

get a reference to text frames by index number, get the first Frame

Set iframe = idoc.TextFrames(1)

change contents

iframe.Contents = "Hello World from Excel VBA!!"

also, you could get a reference to Named text frames like this

Set iframe = idoc.TextFrames("frame2")

Participant
June 22, 2016

Hi Carlos,

Thx a lot,

Can you help me again?

How can I fix this problem ?, the text is more larger than contents. 

September 9, 2015

Hi Carlos,

Is there anyway we can get the Illustrator alphanumeric data to excel.Suppose my illustrator file has call outs in the .ai file can i get them in to excel sheet

CarlosCanto
Community Expert
Community Expert
September 9, 2015

Hi, yes it's possible, see Lesson 4, post # 15

September 10, 2015

Hi Carlos,

Good Afternoon,

Thanks for the quick reply.

Can i have the code to read the text available and write it to excel .Please support me on this.I need the code as i am working on a model here where all the illustrator call outs are listed in a excel to find out missed and repetitive call out or i need a script which converts .ai file to text .txt file.

Your help is highly appreciated.

It would be great if i get the code today by EOB hours.

Thanks and regards,

Bhanu

suneelkv
Known Participant
June 17, 2015

Hi Carlos & Group,

I couldn't do the fourth step

Open Excel

- hit Alt+F11, to bring up the editor

- in the Tools menu, click on References...

- add a reference to "Adobe Illustrator CS5 Type Library"



In Tools ->References I couldn't find this library.

How to proceed further?



suneelkv
Known Participant
June 17, 2015

Got it working sorry please ignore my last post I was trying on a machine which doesn't have Adobe Illustrator installed....

September 25, 2013

Carlos! Thank you so much for all the different work that you have put on these forums. It really is a lifesaver for many I am sure!

I am looking to do something exactly like what you did in post 11 of the following forum: http://forums.adobe.com/thread/773143

But since I am more of a beginner with scripting I figured I'd try this out and already having what looks like software issues. I did what you instructed in the first post of this forum, however when I hit run, the window moved over to Illustrator and then never did anything and when I went back to the VBA Editor in Excel it showed the error message below:

When I hit Debug, it highlights the following line:

Set idoc = iapp.ActiveDocument

So I searched the error on google, but I couldn't find anything specific to working with Excel and Adobe Illustrator, all of the other errors seemed to be with other applications. I saw one forum where it showed this error because two versions of Excel were installed, which I did have that (2010 and 2013) but I used a different hard drive that only has Excel 2010 and it still didn't work.

I am using Windows 7 64 Bit with Adobe Illustrator CS6. The second hard drive I tried with only one version of Excel is a brand new clean install of Windows 7 that has not been updated. The other hard drive is also Windows 7 but has been updated. Both show the same error.

Let me know if you need any more info and if you can help me I am really excited to get stuff like this running in the future as this could save myself and my colleagues a ton of time.

ThinkingThings
Inspiring
September 25, 2013

Hi tyedye09wab,

I don't mean to step on Carlos' conversation with you, but in case he is busy, I thought that I would throw in a quick comment or two.

First - to answer your question -- did you have an active document "open"? If there is no document open, it can't assign the variable to a non-open document. That would involve code to create a document.

Hope this helps,

TT


ThinkingThings
Inspiring
September 25, 2013

Sorry - forgot point #2.   Do you have any experience in Visual Basic? If so, while it could be considered a "one-off" way to get to Illustrator scripting, Visual Studio/VB does provide Intellisense for the entities that should help you in your learning process. I am not sure what Visual Studio Express provide in the way of Intellisense, but at least the Pro version does. I would like to think that even the Express version would do the trick for you. My thinking had always been that sInce you have to have Excel open to run VBA anyway, may as well have Intellisense and a top-rate debugger to help you. Now having said all of that, if Excel/VBA offers intellisence, then there may not be much of an advantage to many, but I find that working in VB gives me a whole new level of control outside of AI scripting that I can use to integrate with the AI scripting.

JMHO.

Take care,

TT

Inspiring
March 22, 2012

When can we expect the next class session to begin?

CarlosCanto
Community Expert
Community Expert
March 22, 2012

Lesson 2 - Move the Hello World to the center of the page

to move our "Hello World" textFrame to the center of the page, we need to know where the center is first...one way to do it is to get the Width and the Height of the document and divide by two to get half of it...let's assign those values to our variables to get to use them later in the code.


docWidth = idoc.Width

docHeight = idoc.Height

now we need to move our text to this new position, but there's no property to get the center of our text frame...we only have top and left properties, so we use the width and height properties and divide by two like we did with the document.


frameWidth = iframe.Width

frameHeight = iframe.Height

once we have both the document and frame measurements, we assign them to the new frame position


iframe.Top = (-docHeight / 2) + (frameHeight / 2)

iframe.Left = (docWidth / 2) - (frameWidth / 2)

the whole code looks like this, please note that Illustrator reversed the Y axis in CS5, for CS4 it will go the opposite direction.

Sub helloWorld2()

    Dim iapp As New Illustrator.Application

    Dim idoc As Illustrator.Document

    Dim iframe As Illustrator.TextFrame

    Set idoc = iapp.ActiveDocument

    Set iframe = idoc.TextFrames.Add

    docWidth = idoc.Width

    docHeight = idoc.Height

    iframe.Contents = "Hello World from Excel VBA!!"

    frameWidth = iframe.Width

    frameHeight = iframe.Height

    iframe.Top = (-docHeight / 2) + (frameHeight / 2)

    iframe.Left = (docWidth / 2) - (frameWidth / 2)

    Set iframe = Nothing

    Set idoc = Nothing

    Set iapp = Nothing

End Sub

to be continued....

Inspiring
March 23, 2012

Excellent lesson Carlos! I've been trying to get my head around alignment for awhile.

Inspiring
March 17, 2012

That is a great start Carlos! I'll give it a go and let you know. Thanks!

Inspiring
March 17, 2012

Carlos- I tested this and got it to work. I'm a little familiar with VBA in Excel within Excel and that is it. That is sort of what  got me into scripting. I started messing around with JS because it was cross-platform. I understand that DIM is the same as VAR in JS and that SET adds a reference to the object. I also understand that "set object = nothing" frees up memory, correct? With that said, I know you don't want to move too fast with my 101, but I have a question.

In my last discussion about getting illustrator to read from an excel csv, I was wondering if the same thing could be done with Excel.

Could an XLSM file read a line from a CSV file in "A1" and depending on what was in the first column in the XLSM, search the CSV file and find the indexed item in the second column or third? I've done it before with standard VLookup functions and it was cumbersome and very slow, and for some reason, everytime I would save the file it would take forever although the xls file was a single sheet.

i've attacthed a sketch of what I'm talking about, because it's hard to explain and I think the sketch explains it better. Here it is, sorry if it is blurry, it looked fine before I posted:

I apologize to ask a non-Adobe question on their forum, but you are a big help and the guys I've come across on Excel forums have been very condesending. I'll eventually get to the step of saving to ADOBE PDF from Excel, but not just yet. Thanks in advance!

CarlosCanto
Community Expert
Community Expert
March 17, 2012

I understand that DIM is the same as VAR in JS and that SET adds a reference to the object. I also understand that "set object = nothing" frees up memory, correct? With that said, I know you don't want to move too fast with my 101, but I have a question.

all that is correct, including moving to fast in this post.

I'm a little familiar with VBA in Excel within Excel and that is it. That is sort of what  got me into scripting. I started messing around with JS because it was cross-platform.

me too, Basic got me hooked to programming, it was ok for my own needs, then I moved to JS to do cross-platform scripting.

Could an XLSM file read....?

yes, it can. Let's do this, to try to keep this organized, start a new thread with your more advanced question and I'll try to help you there. You can copy/paste from here to the new thread.