MS Word Text Box Find and Replace

C

call_me_sol

Hi -
First, let me say that I have absolutely no VB experience. Here's
what I'm trying to accomplish (and it doesn't really sound too
difficult for you VB experts out there :))

Using MS Word, I have created a text box (created with the Control
Toolbox)
Ideally, I will create five individual text boxes to be associated
with specific parts of the document.

In the same document, I have text that is made up of numbers and
letters. Certain parts of the text repeat (for example, the string
112233 is repeated in several places in the document). I want to
associate one text box with that string (112233) so that if a user
inputs new text in the text box, it will find and replace the string
(112233) and replace it with the new text from the text box.

Example (the template that the user will see when opening document):
Text box: <CURRENTLY BLANK> (associated with the 112233)
Text box 2: <CURRENTLY BLANK> (associated with JoesGarage)
Paragraph: 112233-JoesGarage term ACCEPT from community L3VPN

User inputs data into the text box:
Text box: 009988 (associated with the 112233)
Text box 2: FranksFishShack (associated with JoesGarage)
Paragraph: 009988-FranksFishShack term ACCEPT from community L3VPN

Please tell me that this can easily be done using VB.

Thank you.
 
C

call_me_sol

WOW -- Thank you Doug, I have been trying this for about a week now
and I have had absolutely NO luck!!! ThANK YOU!

I have two more questions and my form will work perfectly

1) RE: The box that pops up when I open the form. I need this to
happen about five times -- each time, the box will replace a different
part of text in the document. How can I make the box pop up multiple
times?

2) It seems that after the replacements are made in the doc, there is
a trailing space (where the bookmark brackets were) that isn't being
removed. This is causing a problem with the text formatting. Can the
training space be removed?

Thank you again -- you have been a tremendous help!
 
G

Greg Maxey

What to you mean by popping up multiple times? Do you want five boxes to
pop up when the form opens? Do you want them to randomly pop up? Why don't
you use just one userform with five text boxes in the form and let your
users fill it all out at one time?
 
C

call_me_sol

You're absolutely right, and I did just that. One userform with
several replacement boxes.

So, now, the last two problems:

1) The trailing space after the bookmark brackets is causing a
problem.
2) The text is being inserted before (probably b/c of the
InsertBefore command :)) instead of replacing the stuff in the
bookmark. I want to have the bookmark stuff replaced.

Last two things, and I'm all set!

Thanks again everyone!!!
 
G

Greg Maxey

Change the command buttton click code to something like this:

Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
Set oRng = oBMs("Text1").Range
oRng.Text = TextBox1
oBMs.Add "Text1", oRng
Set oRng = oBMs("Text2").Range
oRng.Text = TextBox2
oBMs.Add "Text2", oRng
Set oRng = Nothing
Set oBMs = Nothing
Unload Me
End Sub
 
C

call_me_sol

I have absolutely no idea what the heck I just did, (errr, what YOU
just did) but IT WORKS!!!!!!!

Thank you, thank you, thank you!!!! You have been an absolute life
saver!!!!!!!
 
C

call_me_sol

Well, now that I have everything in place, I discovered something
else.

I can only assign a bookmark to one phrase-- i.e., if I use Bookmark1
to refer to Text1, I cannot use Bookmark1 to refer to Text1(a) in a
different spot in the same document. Instead, I have to create an
additional Bookmark2 to refer to Text1(a) . Then, in the VBA Code, I
have the following:

Set oRng = oBMs("Text1").Range
oRng.Text = TextBox1
oBMs.Add "Text1", oRng
Set oRng = oBMs("Text1a").Range
oRng.Text = TextBox1
oBMs.Add "Text1a", oRng

So, you'll notice that I have the contents of TextBox1 replacing both
"Text1" and "Text1a". Is there any way to make the TextBox1 replace
both fields without creating several bookmarks? Right now, I have
created about 20 bookmarks to replace five things.

Thanks again!
 
D

Doug Robbins - Word MVP

Instead of using bookmarks, use document variables.

In the code in the userform, use

With ActiveDocument
.Variables("varname1").Value = TextBox1.Text
.Variables("varname2").Value = TextBox2.Text
.Variables("varname3").Value = TextBox3.Text
.Variables("varname4").Value = TextBox4.Text
.Variables("varname5").Value = TextBox5.Text
.Fields.Update
End With

and in the Template, wherever you want the Text from TextBox1 to appear,
insert a {DOCVARIABLE varname1 } field. Likewise for 2, 3, 4 and 5. You
must use Ctrl+F9 to insert the field delimiters { } and Alt+F9 to toggle off
their display.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

call_me_sol

OK, great. I have discovered a problem in the last step.

My intention was to take this new *.dot file, and upload it to my
webpage. Then, users could simply click on the file and be prompted
with the text-box to begin their replacements. Unfortunately,
downloaded the *.dot file and tested it (using "save as" and "open
from current location") the text box doesn't pop up to begin
replacements. When the *.dot file is on my local HD, it works as
desired.

Any idea when downloading the file doesn't work?
 
D

Doug Robbins - Word MVP

Could be that the macro security level is set to high and the template that
is being downloaded is not considered as coming from a trusted source so the
macro in it that would cause the user form to be displayed does not work.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

call_me_sol

bump . . .

Anyone have any idea why the template won't work when downloaded from
a web page?
 
C

call_me_sol

Well, I checked the permissions of the site that I'm using, and I'm
pretty sure that the administrator has disabled macros and/or enabled
some anti-virus protection to prevent the macro from working. Am I
now screwed? Is there any way to embed this code in the document
rather than using a macro?
 
C

call_me_sol

OK -- We've used the form for a few days and we need to more things:

1) I don't want the box to disappear after the people generate their
configs. I want the box to stay on the page.
2) I want to add a "CLEAR" Button that clears all data input by the
user (basically, so the user can start over again).


can anyone help?
 

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