Skip to main content
Participant
August 28, 2021
Answered

Illustrator layer scripting with VBA

  • August 28, 2021
  • 3 replies
  • 591 views

Hi there,

 

I am experienced with writing scripts using VBA to operate illustrator. I know VBA is older but I know the language reasonably well. 

 

However I am having a nightmare trying to figure out the layer hierarchy.

 

I need to get into the child layers. How do i do that? I can access the top level layers easily but that's as far as I get.

 

Do I use pageitems?

 

Thanks in advance 

This topic has been closed for replies.
Correct answer CarlosCanto

no, pageItems only refer to objects on the artboard. 

there are no sublayer objects, sublayers are referenced as layers, but the parent object is a layer instead of a document.

 

' get the width of the first item in the first sublayer of the first layer
Sub sublayerTest()
    Dim iapp As New Illustrator.Application
    Dim idoc As Illustrator.Document
    Dim ilayer As Illustrator.Layer
    Dim isublayer As Illustrator.Layer
    Dim pgItem As Illustrator.PathItem
    
    Set idoc = iapp.ActiveDocument
    Set ilayer = idoc.Layers(1) ' first layer
    Set isublayer = ilayer.Layers(1)    ' first layer first sublayer
    Set pgItem = isublayer.PathItems(1) ' first sublayer page Item
    
    MsgBox "PageItem Width = " & pgItem.Width
    
End Sub

 

3 replies

Participant
August 29, 2021

Thank you both. I have managed to get into the layers now.

 

@CarlosCanto I have used a lot of your old posts about scripting with illustrator and VBA to teach myself. Very informative! 

 

 

CarlosCanto
Community Expert
Community Expert
August 29, 2021

you're welcome @defaultp98xeigtlb28 that's the idea, glad you've found it uselful

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
August 28, 2021

no, pageItems only refer to objects on the artboard. 

there are no sublayer objects, sublayers are referenced as layers, but the parent object is a layer instead of a document.

 

' get the width of the first item in the first sublayer of the first layer
Sub sublayerTest()
    Dim iapp As New Illustrator.Application
    Dim idoc As Illustrator.Document
    Dim ilayer As Illustrator.Layer
    Dim isublayer As Illustrator.Layer
    Dim pgItem As Illustrator.PathItem
    
    Set idoc = iapp.ActiveDocument
    Set ilayer = idoc.Layers(1) ' first layer
    Set isublayer = ilayer.Layers(1)    ' first layer first sublayer
    Set pgItem = isublayer.PathItems(1) ' first sublayer page Item
    
    MsgBox "PageItem Width = " & pgItem.Width
    
End Sub

 

Community Expert
August 28, 2021

Layer object has a property layers which is a collection of the child layers. See the following

https://ai-scripting.docsforadobe.dev/jsobjref/Layer/#layer-layers

So you need to iterate this structure recursively to get all the child elements in the hierarchy of a particular layer. The object model should be the same for VB and JS so you can refer the JS DOM documentation and find the corresponding properties/methods for VB. You could also use a tlb viewer to get the VB DOM documentaion.

-Manan

-Manan