Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms


MS Word Macros - Find and Delete Specific Text Boxes
 
Sub RemoveBoxesContainingCertainText()

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

   If aShape.Type = msoTextBox Then

      If InStr(1, aShape.AlternativeText, "draft", vbTextCompare) > 0 Then aShape.Delete
      'The key to this macro is whatever text you choose in place of "draft"

   End If

Next

End Sub

'****************************************************************
'ALTERNATIVE VERSION: DELETE ALL TEXT BOXES REGARDLESS OF CONTENT
'****************************************************************

Sub Remove_ALL_TextBoxes()

Dim aShape As Shape

For Each aShape In ActiveDocument.Shapes

   If aShape.Type = msoTextBox Then

      aShape.Delete

   End If

Next

End Sub


If you have never used macros before, then click here for an "Absolute Beginner's Introduction."
 

What these macros do:

These macros are designed to remove text boxes from a document. The main text of the document, as well as all other parts of the document, will be left unchanged.

"Text box" can be an ambiguous term within the context of Microsoft Word. Therefore, I must clarify which kind of text box will be deleted. The two macros on this web page will delete the type of text box that is created using the "Drawing" toolbar. This is also the same type that can be created by choosing "Insert" then "Text Box" using the main menu bar at the top of the Word application screen. This macro will NOT delete Text Form Fields, which are inserted using the "Forms" toolbar. (If the Drawing toolbar and/or Forms toolbar are not displayed in your Word window and you would like to display them, just right-click while your mouse cursor is over any other Word menu or toolbar, and then select "Drawing" or "Forms" from the list that appears.)

These macros are very short and straightforward. The second macro especially (Sub Remove_ALL_TextBoxes) requires little to no explanation. Remove_ALL_TextBoxes does exactly as I have stated: it will remove each and every text box from a document.

The first macro (Sub RemoveBoxesContainingCertainText), on the other hand, requires clarification on just a few tiny points. First of all, as it is written now, the macro will only delete a text box if the text box contains the word "draft." Note: the function used to find the word "draft" will use a non case-sensitive (or "case insensitive") search. Therefore, if one text box says "draft" and another says "DRAFT" or "Draft," they will all be deleted.

To search for a word or string of characters other than "draft," you only need to make one tiny change to the macro code. Simply delete draft from the fifth line of programming code, and replace it with any word or phrase or character of your choosing. Be sure to leave the quotation marks and the commas exactly as they are. Only replace or change what is between the two quotation marks that originally contained draft.

Also, please be aware of the following: the RemoveBoxesContainingCertainText macro checks to see if your search string (for example, "draft") is found anywhere within the text box. So if one text box says "Draft 1" and another says "Rough Draft," both text boxes will be deleted because the string "draft" was located within the text. This can become progressively more confusing depending on the circumstances of your document. For example, say you have three text boxes that each contain the name of an animal. One says "Cat," one says "Dog," and one says "Catfish." If you run the macro to delete any box with "Cat," then you must be aware that the box with "Catfish" will be deleted also!

So... make sure you use good search logic, and then delete away!

Thanks for reading about my macro!
-Kelly

Comments?

Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Support web pages written to maximize browser compatibility
Click here to learn about web standards Validate your web pages here!
Read this to learn about W3C standards

Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms