Word

Delete Textbox if it contains a pre-defined word

Ease of Use

Easy

Version tested with

2000 

Submitted by:

lucas

Description:

If a textbox (from shape collection) in the document contains the word specified it is deleted when this macro is run. 

Discussion:

You have instructions on a page for anyone opening the document. You wish to be able to remove the textbox with the instructions after they read them. (This sub only deals with a textbox of type "msoTextBox" from Shape collection) 

Code:

instructions for use

			

Put this code In a standard module: Option Explicit Sub RemoveBoxIfItSaysInstructions() Dim aShape As Shape 'Seaches the document for shapes For Each aShape In ActiveDocument.Shapes 'If it finds a textbox then... If aShape.Type = msoTextBox Then 'If it finds a textbox which contains the word instructions then delete it. If InStr(1, aShape.AlternativeText, "instructions", vbTextCompare) _ > 0 Then aShape.Delete End If Next End Sub

How to use:

  1. Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
  2. On the toolbar of the Visual Basic Editor, go to insert - module
  3. In the module pane paste the code above.
  4. Close the Visual Basic Editor by clicking the X in the upper right corner or go to File-Close
 

Test the code:

  1. Put a textbox on your document and type the word Instructions(you can change this in the code)
  2. On the main menu go to tools-macro-macros.
  3. In the dialog window select RemoveBoxIfItSaysInstructions and then click run.
  4. Any textbox with the word instructions will be deleted.
 

Sample File:

Delete_textbox_if.zip 10.01KB 

Approved by mdmackillop


This entry has been viewed 109 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express