MS Office Forum / Word / Programming / March 2007
MS Word Text Box Find and Replace
|
|
Thread rating:  |
call_me_sol@yahoo.com - 24 Feb 2007 19:30 GMT 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.
Doug Robbins - Word MVP - 24 Feb 2007 20:43 GMT I think that you should probably investigate the use of a userform.
See the article "How to create a Userform" at:
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm
 Signature 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
> Hi - > First, let me say that I have absolutely no VB experience. Here's [quoted text clipped - 26 lines] > > Thank you. call_me_sol@yahoo.com - 24 Feb 2007 21:12 GMT 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!
Greg Maxey - 24 Feb 2007 21:34 GMT 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?
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> WOW -- Thank you Doug, I have been trying this for about a week now > and I have had absolutely NO luck!!! ThANK YOU! [quoted text clipped - 12 lines] > > Thank you again -- you have been a tremendous help! call_me_sol@yahoo.com - 24 Feb 2007 21:44 GMT 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!!!
Greg Maxey - 24 Feb 2007 22:13 GMT 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
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> You're absolutely right, and I did just that. One userform with > several replacement boxes. [quoted text clipped - 10 lines] > > Thanks again everyone!!! call_me_sol@yahoo.com - 24 Feb 2007 22:39 GMT 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!!!!!!!
call_me_sol@yahoo.com - 24 Feb 2007 23:07 GMT 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!
Doug Robbins - Word MVP - 25 Feb 2007 08:14 GMT 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.
 Signature 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
> Well, now that I have everything in place, I discovered something > else. [quoted text clipped - 18 lines] > > Thanks again! call_me_sol@yahoo.com - 25 Feb 2007 19:05 GMT 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?
Doug Robbins - Word MVP - 25 Feb 2007 20:44 GMT 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.
 Signature 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
> OK, great. I have discovered a problem in the last step. > [quoted text clipped - 7 lines] > > Any idea when downloading the file doesn't work? call_me_sol@yahoo.com - 25 Feb 2007 23:06 GMT I checked that. My macro level is set to "low". Any other thoughts?
call_me_sol@yahoo.com - 26 Feb 2007 02:38 GMT bump . . .
Anyone have any idea why the template won't work when downloaded from a web page?
call_me_sol@yahoo.com - 26 Feb 2007 03:54 GMT 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?
call_me_sol@yahoo.com - 02 Mar 2007 22:34 GMT 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?
|
|
|