Deleting a Text Box programmatically

  • Thread starter Roderick O'Regan
  • Start date
R

Roderick O'Regan

I have created a Text Box with some text in it and then stored itas an
AutoText item in the template for the active document. I called this
autotext "~questions~". I know how to programmatically insert the
autotext in question onto the page.

However, if the user changes their mind and doesn't want the text box
is there a way of giving it a name, perhaps, and saying to VBA "...go
and find this text box named XYZ and delete it"?

Regards

Roderick O'Regan
 
J

Jezebel

You can give the textbox a name before you define it as an autotext entry.
It will retain that name when inserted, and you can then refer to it by
name.

To set the name, if the textbox is the only shape in the document, use --

ActiveDocument.Shapes(1).Name = "XYZ"


Then you can use

ActiveDocument.Shapes("XYZ").Delete

There's one gotcha: Word doesn't care if there are several shapes with the
same name -- as would happen if you inserted your named autotext textbox
more than once.
 
Top