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.
palmetto1 said: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.
Yeah, I did debug it and knew what line was causing the issue, but I couldn't figure out how to fix it. I guess I should've added that info to my original post.
Didn't know about the Locals window though, thanks for the tip!