That is a great start Carlos! I'll give it a go and let you know. Thanks!
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!
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.
k Carlos, will do!
When can we expect the next class session to begin?
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....
Excellent lesson Carlos! I've been trying to get my head around alignment for awhile.
thanks, more to come..
Lesson 3: Create a new document, create a new layer, transfer data from Excel to AI
In lesson 2, I mentioned illustrator reversed the Y axis. That means that in CS4 and earlier, the origin was at the bottom left of the document and positive Y values went "up" from the bottom. In CS5, the origin moved to the top left corner of the document and positive values went "down" from the top.
are you confused yet? No? let me try harder. In order to keep existing scripts from breaking, if you create NEW documents in CS5, the axis will be at the bottom/left (just like CS4).
got it? cool, let's put our newfound knowledge to good use. In the next exercise, we will create a new document, add a second layer, a text frame to this new layer, and get the data in the first Cell in the Active Excel Document to add to the text frame
to create a new document with defaults, use the add method
idoc = iapp.documents.add
next we will create a new layer
Dim ilayer As Illustrator.Layer Set ilayer = idoc.Layers.Add
and rename our newly created layer
ilayer.Name = "Data from Excel"
then add a text frame to the layer, as opposed to the document in the previous post
Set iframe = ilayer.TextFrames.Add
before we populate our text frame, go to Excel and type "Hello from Cell A1!", in Cell A1 in the Active Book.
declare a variable to hold Cell 1
Dim myRange As Range Set myRange = Range("A1")
now use that to populate the text frame
iframe.Contents = myRange.Value
last, we update the top position, to accommodate for the "new" document issue
iframe.top = (docHeight / 2) + (frameHeight / 2)
here's the complete code
Sub helloWorld3() Dim iapp As New Illustrator.Application Dim idoc As Illustrator.Document Dim iframe As Illustrator.TextFrame Dim ilayer As Illustrator.Layer 'new Dim myRange As Range ' new Set myRange = Range("A1") ' new Set idoc = iapp.Documents.Add 'updated Set ilayer = idoc.Layers.Add 'new ilayer.Name = "Data from Excel" 'new Set iframe = ilayer.TextFrames.Add ' updated docWidth = idoc.Width docHeight = idoc.Height iframe.Contents = myRange.Value 'updated frameWidth = iframe.Width frameHeight = iframe.Height iframe.top = (docHeight / 2) + (frameHeight / 2) 'updated iframe.left = (docWidth / 2) - (frameWidth / 2) Set myRange = Nothing 'new Set ilayer = Nothing 'new Set iframe = Nothing Set idoc = Nothing Set iapp = Nothing End Sub
Excellent, I didn't know that about the origin reversing. That's odd.Thanks
Anymore Carlos, my brain is hungry!!
Hi, thanks for being so enthusiastic about this post, I'll do lesson four soon, I've been a little busy lately...stay tuned!!
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. ☺
I wish I could, I don't think I'm any good at writing...I mean properly.
Lesson 4: Creating Shapes, Working with Selections, writing data to Excel
In the Illustrator world a shape is a....well, I'm not going to bore you with technical terms only rocket scientists would understand....let's just say a Circle is a shape, as well as a Rectangle or a Star, there much better than the actual definition. Then in the scripting lingo all shapes are pathItems.
There are a number of ways of creating shapes, in this exercise well focus on using the various Methods of the PathItem Object.
to create a Circle we'll use the Ellipse Method, all arguments are optional, if we don't supply any, the method uses default values.
([top as Double]
[, left as Double]
[, width as Double]
[, height as Double]
[, reversed as Boolean]
[, inscribed as Boolean])
Dim icircle As Illustrator.PathItem Set icircle = idoc.PathItems.Ellipse(300, 300, 100, 100)
similarly, to create a square, we use the Rectangle Method
Dim isquare As Illustrator.PathItem Set isquare = idoc.PathItems.Rectangle(200, 200, 100, 100)
and to make a star we use the...hum...the Star Method
Dim istar As Illustrator.PathItem Set istar = idoc.PathItems.Star(400, 400, 100, 50, 5)
now lets select the square and read some of its properties and write them down to Excel
isquare.Selected = True
get properties of the top most selection, in case we have many items selected, for now it should only be the square
w = idoc.Selection(0).Width h = idoc.Selection(0).Height y = idoc.Selection(0).top x = idoc.Selection(0).left
and lets write those values to Excel, using the Cells object this time. Make sure you have a blank Excel book open, it will write data to the first 4 rows, 2 first columns
Cells(1, 1) = "width: " Cells(1, 2) = w Cells(2, 1) = "height: " Cells(2, 2) = h Cells(3, 1) = "top: " Cells(3, 2) = y Cells(4, 1) = "left: " Cells(4, 2) = x
here's the complete code, from now on, we'll start every exercise with a blank Excel book and a blank Illustrator document, so please do that before runing.
Sub lesson4shapes() Dim iapp As New Illustrator.Application Dim idoc As Illustrator.Document Dim icircle As Illustrator.PathItem Dim isquare As Illustrator.PathItem Dim istar As Illustrator.PathItem Set idoc = iapp.ActiveDocument Set icircle = idoc.PathItems.Ellipse(300, 300, 100, 100) Set isquare = idoc.PathItems.Rectangle(200, 200, 100, 100) Set istar = idoc.PathItems.Star(400, 400, 100, 50, 5) isquare.Selected = True w = idoc.Selection(0).Width h = idoc.Selection(0).Height y = idoc.Selection(0).top x = idoc.Selection(0).left Cells(1, 1) = "width: " Cells(1, 2) = w Cells(2, 1) = "height: " Cells(2, 2) = h Cells(3, 1) = "top: " Cells(3, 2) = y Cells(4, 1) = "left: " Cells(4, 2) = x Set istar = Nothing Set isquare = Nothing Set icircle = Nothing Set idoc = Nothing Set iapp = Nothing End Sub
Note that the code we just wrote is not the most efficient way of doing things, we don't have to select an object in order to work on it (get the properties for instance). We did it for illustration purposes, we could also use a loop to write data to Excel. We'll do that in the next lesson.
also, note that the top/left values don't match exactly with the values we entered (200, 200), homework, can you tell why?
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.
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
Got anymore Carlos?
thanks for your interest, more will come for sure...shortly.
Alright Carlos. I eagerly await your next lesson!
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?
correct, top/left properties reflect visible bounds (includes stroke size), what we see in the UI when we select an item is Geometric Bounds in scripting.
thanks for posting, you get an extra credit
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.
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,
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.
hi TT thanks for commenting, VBA does have intellisence and code completion. VBA is nice, easy to use and "on-hand" in most office environments to get people introduced to programming...
...if the script runs without a document open, it will raise a "no such element" error, it seems tyedye is having issues connecting Excel to Illustrator
tyedye, try this, close Illustrator, then go to your Illustrator Shortcut, right-click on it and click on "Run as administrator"...then try running you VBA code.
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?
try removing the reference to the Illustrator Type Library, run as administrator, then re apply the Type Library
how about this test, does it run?
Sub basicTest() Dim iapp As New Illustrator.Application MsgBox iapp.Name End Sub
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:
I tried running Illustrator in compatibility mode and I got a different error when I did that:
Run-time error '-2147024156 (800702e4)':
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?
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!
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?
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...
it supposed to be as simple as I described it in post #1,
if you have other Office Applications try making a simple application.name test from Excel to Word or Outlook, if that works then Office installation is ok and you may need to reinstall Illustrator with antivirus off...just a thought.
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?
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
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!
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
indeed good news, thanks for posting your findings...could it be possible the 64 bit version has its own Type/Object Library?