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 ShapeDim p As PageDim 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 IfNext pActiveDocument.Pages(1).ActivateEnd Sub
try code below
added single undo
warning when all docuemnt pages have to be deleted
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.CountSet ArtisticText = p.Layers(1).CreateArtisticText(33, -4, ItemQty, cdrLanguageNone, cdrCharSetSymbol, "Bookman Old Style", 400)End Ifp.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 Thenp.DeleteEnd IfNext p
mye:ActiveDocument.EndCommandGroup
ActiveDocument.Pages(1).ActivateEnd Sub
Thanks Mek, worked great!
You're welcome.