VBA to search for page layer

I am trying to count the number of shapes on the layer "LayerForTextBox" on each page that has that layer. I keep getting the error that it cannot find that layer (which is true, some pages don't have that layer), but I thought I had accounted for that in my If Then statement.  What am I doing wrong?

Sub Test()

Dim ArtisticText As Shape
Dim p As Page
Dim ItemQty As Integer

For Each p In ActiveDocument.Pages
If Not p.Layers("LayerForTextBox") Is Nothing Then
ItemQty = p.Layers("LayerForTextBox").Shapes.count
Set ArtisticText = p.Layers(1).CreateArtisticText(33, -4, ItemQty, cdrLanguageNone, cdrCharSetSymbol, "Bookman Old Style", 400)
End If
p.Layers(2).Activate
If p.Layers.count < 5 Then
p.Delete
End If
Next p
ActiveDocument.Pages(1).Activate
End Sub

Parents
  • I keep getting the error that it cannot find that layer (which is true, some pages don't have that layer), but I thought I had accounted for that in my If Then statement.  What am I doing wrong?

    At some point, when things aren't working the way you think they should, it's time to bring some of the debugging tools into play.

    You can do a lot just by using break points, "stepping through" the execution of the code one line at a time, and using the Locals window to see what's going on.

Reply Children
No Data