so you have a VBA script already and need to turn it into VBscript?
Hi, well, I'm running a script in Solidworks, shelling a the outputted .dxf
file in AI, and then continuing to run the code in AI. Problem is, after
the shell command executes, the file opens up, but then the open file isn't
seen by the remaining code.
Here's what I have:
swModel.SaveAs3 "" & sname & "", 0, 0 'This is the 'save to DXF'
command, with sname having a .dxf extension.
ShellExecute 0, "open", sname, vbNullString, vbNullString,
'Here the AI code starts.
Set appRef = CreateObject("Illustrator.Application")
Set doc = appRef.ActiveDocument
Set newlayer = appRef.ActiveDocument.Layers.Add
'Code doesn't work if there isn't a document already open in
illustrator. The shelled document opens, but then I get a debug error, "No
The code works if there is a document already open when I run the script.
did somebody else write the script? are you comfortable with scripting? I'm just trying to find out how much help you need
to begin with, comment out (or delete) this line, to avoid the "No such element" error
'Set doc = appRef.ActiveDocument
that should take or the error
I still get the error even with that line commented out. Yes, it is my
script, and I am comfortable writing in VBA for Solidworks. Extending that
to Illustrator is something new for me. VBscript doesn't appear to be much
different, but I'm not sure how to proceed moving from one application to
another within the same macro.
I don't know how solidworks interacts with Illustrator, maybe ShellExecute is opening a second instance of illustrator? and the file hasn't finished opening and the code just jumped to the following lines
try making a new module, and have the existing illustrator instance open the dxf file directly, just to see if it works...if it does, we'll go from there.
I use this style in my VBA code
Dim appRef As New Illustrator.Application Dim doc As Illustrator.Document Set doc = appRef.Open("your dxf file name"); Set newlayer = doc.Layers.Add
Thanks, that seemed to do the trick, although the Illustrator window
doesn't come to the front. I think you're right that the code seemed to be
jumping to the following lines.
Carlos, thanks for the help. I noticed you've written some scripts working
with color, layers, for paths, etc. I'd like to select paths in a document
by a particular color, and move them to a new layer. I thought I could
stitch together some code, but it gets stuck. Maybe you have an idea...
Dim iapp As New Illustrator.Application
Dim idoc As Illustrator.Document
Dim ilayer As Illustrator.Layer
Dim ipath As Illustrator.PathItem
Set idoc = iapp.ActiveDocument 'get active document
Set ilayer = iapp.ActiveDocument.Layers.Add
ilayer.ZOrder aiSendToBack 'move to the bottom
'Here I try to create a color:
Set colorref = CreateObject("Illustrator.RGBColor")
colorref.Red = 128
colorref.Green = 128
colorref.Blue = 128
For i = idoc.PathItems.Count To 1 Step -1 'loop thru all pathItems
Set ipath = idoc.PathItems(i) 'get path
'Here I get an errror: "Property is write/only"
If ipath.StrokeColor = colorref Then
ipath.Move ilayer, aiPlaceAtBeginning
' If ipath.StrokeWidth = 3 Then 'check for size
' ipath.Move ilayer, aiPlaceAtBeginning 'move to specificLayer
if size = 3 points
' End If
'move layer back to its original position
ilayer.Move idoc.Layers(ilayerzorder), aiPlaceBefore
Set ipath = Nothing
Set ilayer = Nothing
Set idoc = Nothing
Set iapp = Nothing
hmm, I'm getting an error comparing colors, I had to go down to the actual r, g, b values, and even then I had to put the values outside of the if-then (I got a "too complex error")
Sub compareColors() Dim iapp As New Illustrator.Application Dim idoc As Illustrator.Document Dim icolor As New Illustrator.RGBColor Dim colorRef As New Illustrator.RGBColor Dim ipath As Illustrator.PathItem Set idoc = iapp.ActiveDocument Set ipath = idoc.Selection(0) icolor = ipath.StrokeColor colorRef.Red = 128 colorRef.Green = 128 colorRef.Blue = 128 cred = 128 cgrn = 128 cblu = 128 ired = Round(icolor.Red) igrn = Round(icolor.Green) iblu = Round(icolor.Blue) If ired = cred And igrn = cgrn And iblu = cblu Then MsgBox "same color" Else MsgBox "different color" End If Set colorRef = Nothing Set icolor = Nothing Set ipath = Nothing Set idoc = Nothing Set iapp = Nothing End Sub
I'm getting an error with the line:
icolor = ipath.StrokeColor "run time error 13, type mismatch"
forgot to say, select ONE path before running
Yeah, that's what I assumed, but l get the error nonetheless.
do you have Excel? do you mind trying it there? could it be solidworks that's acting up?
Yeah, same error with Excel.