Skip navigation
Currently Being Moderated

Illustrator VBA scripting 101 - via Excel

Mar 17, 2012 12:23 PM

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?

 
Replies
  • Currently Being Moderated
    Mar 17, 2012 12:58 PM   in reply to CarlosCanto

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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2012 3:29 PM   in reply to duhwellhuh

    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:

    xlsm example.PNG

     

    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!

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2012 4:35 PM   in reply to CarlosCanto

    k Carlos, will do!

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 22, 2012 4:51 AM   in reply to CarlosCanto

    When can we expect the next class session to begin?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 23, 2012 2:20 AM   in reply to CarlosCanto

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

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 24, 2012 3:55 AM   in reply to CarlosCanto

    Excellent, I didn't know that about the origin reversing. That's odd.Thanks

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 28, 2012 5:01 PM   in reply to duhwellhuh

    Anymore Carlos, my brain is hungry!!

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 30, 2012 7:01 AM   in reply to CarlosCanto

    No problem, I love to learn.

     

    Yeah, I haven’t seen you on any posts lately. I was wondering what happened. Hopefully you were writing a how to book. ☺

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 6:07 AM   in reply to CarlosCanto

    Oh, that’s a good lesson. Thanks a lot for this one! I wish there was a way for me to give you points on this forum.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 10, 2012 2:35 AM   in reply to CarlosCanto

    Hey Carlos-

     

    I have a grasp on the shapes above, but how about drawing shapes from points? I've went through some tutorials that came with Illustrator VBScript, and I've figured out how to get the sizes right, but when I try to position them, the values that Illustrator show are wrong when I use methods lIke .top and .left for the x and y coordinates. I would assume that it would be an array, but is there a quick way to absolute reference where you want to draw something?

     

    If you have this saved for a future lesson, I can wait, no problem. thanks

    
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2012 3:34 PM   in reply to CarlosCanto

    Got anymore Carlos?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 21, 2012 3:47 PM   in reply to CarlosCanto

    Alright Carlos. I eagerly await your next lesson!

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 3:44 PM   in reply to CarlosCanto

    note that the top/left values don't match exactly with the values we entered (200, 200), homework, can you tell why?

     

     

    Is that because the shape was drawn with the default 1 pt stroke and the .top & .left values are based on visible Bounds?  (my only guess)  So why is that?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 25, 2013 11:44 AM   in reply to CarlosCanto

    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:

     

    SS of Active X Error.jpg

     

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 25, 2013 12:27 PM   in reply to tyedye09wab

    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

     

    
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 25, 2013 12:35 PM   in reply to ThinkingThings

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2013 4:07 AM   in reply to CarlosCanto

    Thank you for the quick responses TT and Carols. I really appreciate that.

     

    I still cannot get this thing running. Yes I have created a blank document in Illustrator. I would just goto File ->New and use the default settings to create a new document, the default settings selected were the Print Profile (I dont know if that makes a difference or not). Is there anyway that I would have to make a new document active? I know in excel just having it open means it is active correct?

     

    Carlos, I tried the Run as Administrator and as soon as I read that I thought that could do the trick, but nope, same thing. Also, I realized that I have both a 32bit and 64bit version of Illustrator, so I tried it in both versions as administrator, still same error. Both versions of Excel are 32bit and I had read that the 64bit versions were lacking in some of the VBA stuff so I think I should be safe there. On a side note I tried the other code you posted on the other forum regarding translation (post 11 of http://forums.adobe.com/thread/773143) after creating a document with those few text layers and the same error came up on the same line of code (Set idoc = iapp.ActiveDocument).

     

    TT, although I said I am a beginner to VBA, that might not be entirely true, but I like to error on the side of more beginner because my experience has more to do with editing code other people have previously made than actually making it from scratch myself.

     

    Any other recommendations?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2013 11:27 AM   in reply to CarlosCanto

    I tried removing and reapplying the Type Library, still did not work. I tried elevating the Illustrator library as well but that didnt do anything.

     

    I just tried this basic test and it still does not work, same error, but it occurs on:

     

    MsgBox iapp.Name

     

    I tried running Illustrator in compatibility mode and I got a different error when I did that:

     

    Run-time error '-2147024156 (800702e4)':

     

     

    Automation error

    The requested operation requires elevation.

     

     

    When I run the script, if I have windows open on top of Illustrator, those windows dissapper and it looks as though the computer just stops on Illustrator for about 30 seconds until the Error comes up.

     

    I thought perhaps it could be Avast! Antivirus blocking any scripts trying to run, so I turned everything off (while not connected to any networks) and tried to run the script and same ol Error 429 ActiveX Error. I dont have any other ideas besides doing another fresh install of office and CS6 on another HD but I really dont want to do that.

     

    Any other ideas?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2013 11:48 AM   in reply to tyedye09wab

    tyedye09wab,

     

    

     

    I (probably we) applaud your persistence. Don't give up. It will be worth your while. We've all been through it to one degree or another.

     

    I don't have a lot of experience in VBA to help you here too much, although (and Carlos will be REALLY excited about this since he's been trying to recruit me to this platform for sometime ) I actually did do a real fast setup with VBA in Excel to Illustrator and it was pretty much a cakewalk. I know that might not be exactly what you wanted to hear, but it goes to prove that it IS doable. I even googled some 429's for you but what I found was more for Office 2007 and prior, so probably not much help there.

     

    Again - here is just a thought (because we REALLY want to see you get past this) -- I wonder if you have any other CS6 components (like InDesign) that you could just reference the type library for that and give the old "Hello World" one more college try. If that were to work, maybe it is a corrupted CS6 Illustrator type library. Just thinking...

     

    HANG IN THERE!

     

    TT

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2013 1:19 PM   in reply to ThinkingThings

    Thanks TT. Im really trying and I want this to work...I think I've given it more that the "good ol college try" but I will press on.

     

    It seems to be something on the Adobe side of things, and Im wondering if I didnt install all of the features or something originally.

     

    When I looked at photoshop (again I know this isnt Illustrator, but maybe there are differences between CS5 and CS6 we are not taking into account) there was this little article that mentioned CS6 not having some of the plugins that were previously available in CS5 and earlier. Is it possible I need to install some kind of add on to Illustrator?

     

    http://helpx.adobe.com/photoshop/kb/plug-ins-photoshop-cs61.html#id_14 532

     

    Thanks again,

     

    TyeDye

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 26, 2013 2:33 PM   in reply to tyedye09wab

    I don't think so. At quick glance it looks like it has something to do with TWAIN drivers. I think that we will find it to be something simple. Problem is, finding the simple solution sometimes takes way longer than it should. I'll keep thinking...

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2013 3:14 AM   in reply to CarlosCanto

    Carlos,

     

    I dont know exactly what you mean when you say a simple application.name test from Excel to Word or Outlook. Do I have to open a reference library like adobe products?

     

    Could you give me a simple sample code to try with excel and word?

     

    TyeDye

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 23, 2013 1:37 AM   in reply to tyedye09wab

    HELLO AGAIN!

     

    So I am pretty excited because I finally got it working!

     

    I had to do a clean install of Windows and so I tried it out as soon as I installed CS6 before updating anything (including Windows Updates) and it seems that the error I got regarding the ActiveX Component only occurs in 64bit Illustrator while the 32 bit Illustrator works fine. I had tried the 32bit version on my other hard drive and had tried this without getting it, but for some reason it works on the cleaner install. I am using Excel 2013.

     

    I am running Windows 7 Pro SP1 64Bit

    8GB RAM

    Intel i7 @2.90GHZ

     

    I still have my older HD, but do not currently have access to it so I will try what I just did later and keep you all updated...Im excited that I am back in the game!

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 23, 2013 6:16 AM   in reply to tyedye09wab

    Great, tyedye09wab! We've been waiting for this good news email! I might suggest that you start a new thread if you have any further problems, just to keep this one less contaminated and folks will be able to focus on your new problems, if any. Anyway - welcome back -- and congrats on your persistence. -TT

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)