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
  • try code below

    added single undo

    warning when all docuemnt pages have to be deleted

    Sub Test()

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

    ActiveDocument.BeginCommandGroup ("test") 'single undo

    For Each p In ActiveDocument.Pages

    If Not p.Layers.Find("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 ActiveDocument.Pages.Count = 1 And p.Layers.Count < 5 Then MsgBox ("All pages to be deleted," & vbCr & "but document must contain min. one page!"): GoTo mye:

    If p.Layers.Count < 5 Then
    p.Delete
    End If
    Next p

    mye:
    ActiveDocument.EndCommandGroup

    ActiveDocument.Pages(1).Activate
    End Sub

Reply Children