From: Felix on 19 Apr 2010 14:44 Hello, I need to find and delete a text box in a document. I was able to record a macro that does this: ActiveDocument.Shapes("Group 3").Select Selection.Delete It works fine if the text box is present, but if it's not there, the macro gives me a run-time error (specified item not found). How can I wrap the above statements to test for the presence of the text box first? Thanks!
From: Jay Freedman on 19 Apr 2010 15:07 Felix wrote: > Hello, > > I need to find and delete a text box in a document. I was able to > record a macro that does this: > > ActiveDocument.Shapes("Group 3").Select > Selection.Delete > > It works fine if the text box is present, but if it's not there, the > macro gives me a run-time error (specified item not found). > > How can I wrap the above statements to test for the presence of the > text box first? > > Thanks! You can't exactly "wrap" that code, but you can test for the presence of any specific named object with code like this: Dim myShape As Shape, tmp As Shape For Each tmp In ActiveDocument.Shapes If LCase(tmp.Name) = "group 3" Then Set myShape = tmp Exit For End If Next If Not (myShape Is Nothing) Then myShape.Delete End If The idea is to iterate through the document's Shapes collection until you find the one with the right name, or until you've tried them all and failed (in which case myShape will be Nothing). Using the LCase function and comparing to an all-lower-case version of the name will make the comparison case-insensitive, which is optional. -- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
|
Pages: 1 Prev: Determine the page number in Word VBA Next: alignment issue in a document |