VBA to Select a Text Box

B

Brenda A. Reid

WinXP
Word 2002

I am in the process of creating a "stamp". What it does is insert a text
box in the upper right corner of your document, shaded light grey, with the
user's choice of COPY, DRAFT, URGENT, ETC in this text box.

The macro:

1. Inserts this stamp in the upper right corner
2. Sends document to print
3. Deletes the stamp so that the document is back to the original form
before running the macro.

It is step 3 that I cannot get to work. VBA wants to know the "Text Box
No." which I don't have or it could be different every time the macro is
run. Is there a way I can have VBA search for a text box or maybe just have
VBA undo the last command (which would be inserting the text box)?

Brenda A. Reid
(e-mail address removed)
 
D

David

I think the answer is to name the text box uniquely as
you create it as in the following simple macro.

Sub InsertTextBox()
ActiveDocument.Shapes.AddTextbox
(msoTextOrientationHorizontal, 410.4, _
28.8, 122.4, 57.6).Select
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(192,
192, 192)
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Name = "MyStampBox"
End Sub

Sub DeleteTextBox()
ActiveDocument.Shapes("MyStampBox").Delete
End Sub
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Brenda A. Reid > écrivait :
In this message, < Brenda A. Reid > wrote:

|| WinXP
|| Word 2002
||
|| I am in the process of creating a "stamp". What it does is insert a text
|| box in the upper right corner of your document, shaded light grey, with
the
|| user's choice of COPY, DRAFT, URGENT, ETC in this text box.
||
|| The macro:
||
|| 1. Inserts this stamp in the upper right corner
|| 2. Sends document to print
|| 3. Deletes the stamp so that the document is back to the original
form
|| before running the macro.
||
|| It is step 3 that I cannot get to work. VBA wants to know the "Text Box
|| No." which I don't have or it could be different every time the macro is
|| run. Is there a way I can have VBA search for a text box or maybe just
have
|| VBA undo the last command (which would be inserting the text box)?
||

Create the stamp and run some code to give them a name and save them as
autotext.
When you insert them with your macro (by inserting the autotext), you will
already know their names... So it will be easy to delete them.

Use this to give them a name:
(Just change the name of the Constant every time you assign a name to a
shape.)
'_______________________________________
Sub NameTextBox()

Const BoxName As String = "COPYBox"

Selection.ShapeRange(1).Name = BoxName

End Sub
'_______________________________________

and this to delete them:

'_______________________________________
Sub DeleteTextBox()

ActiveDocument.Shapes("COPYBox").Delete

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top