Solved

VBA function: loop to replace bookmarks with VBA TextBox value

Posted on 2003-11-14
9
2,050 Views
Last Modified: 2008-01-16
Argh ... it's been too long since I programmed!

I have a Word form and need to copy info entered in a VBA Textbox (say, ClientName) to various places in the document (say, each page). I have added bookmarks where I need it copied to and named them ClientName1, ClientName2, ClientName3, and so on.

So, I need a function that goes through the document looking for ClientName bookmarks, and replacing them with the text from ClientName. I've already stolen--er, was inspired by--Dreamboat's code to replace bookmarks (following), but am having difficulty combining it with the looping functionality.

A nice-to-have: since I'll be naming my VBA TextBoxes the same as my bookmarks, I'd like to not hardcode the textbox name -- I can just grab the name of the current textbox and add 1, 2, 3, etc. to it. Therefore, I picture calling the function by merely typing something like PopulateItFunction Me, Me.Value

Am I dreaming?

Tristan

Dreamboat's bookmark replacement code:

Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
    Dim BMRange As Range
    Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
    BMRange.Text = TextToUse
    ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
0
Comment
Question by:TristanRyan
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 37

Assisted Solution

by:Joanne M. Orzech
Joanne M. Orzech earned 35 total points
Comment Utility
I think it might be a lot easier to just use doc variables instead of bookmarks...but that's my personal preference.

Then you would use code like this:

Dim Client1 as String

ActiveDocument.Variables.Add Name:="Client1", value:=frmForm.Textbox1.Text




HTH
JO
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
oops...sorry - to elaborate

in your document itself, rather than bookmarks, you would Insert, Field, DocVariable, and name it "Client1"
0
 
LVL 14

Expert Comment

by:Tommy Kinard
Comment Utility
Sub UpDateFields(ClientName as Textbox)
    ActiveDocument(ClientName.Name & "1").Result = ClientName.Text
    ActiveDocument(ClientName.Name & "2").Result = ClientName.Text
    ActiveDocument(ClientName.Name & "3").Result = ClientName.Text
    'Continue the above pattern until all are filled
End Sub

Sub UpDateFields(ClientName as Textbox)
    For I = 1 to 3 '<- change the 3 to the total number of ClientName fields
        ActiveDocument(ClientName.Name & Cstr(I)).Result = ClientName.Text
    Next I
End Sub

Let me know if more is needed
dragontooth


0
 
LVL 22

Assisted Solution

by:Dreamboat
Dreamboat earned 25 total points
Comment Utility
Yeah. And you only need one bookmark. Make the rest of them cross-references to the bookmark.

(Dragon--please validate my statement? I think I'm correct.)

Tristan: I don't write code. I steal it. Wherever that bookmark update code came from, I sure didn't write it! But thanks for the credit anyway! LOL
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 65 total points
Comment Utility
<Make the rest of them cross-references to the bookmark>
Yes this can be done with cross reference the draw back is the user will have to Update Fields, which could lead to problems when the user forgets to update. But of course we can code that in also.

Sub UpDateFields(ClientName as Textbox)
        ActiveDocument(ClientName.Name).Result = ClientName.Text '<- This line changed since there is only 1 formfield
        ActiveDocument.Fields.Update
End Sub

dragontooth

0
 
LVL 1

Author Comment

by:TristanRyan
Comment Utility
Well, thanks y'all... I've used a combo of everything. I don't know how one normally gives points out -- if I've committed a faux pas in dividing these points this way, please let me know and I'll post some more points to correct it!

Tristan
0
 
LVL 14

Expert Comment

by:Tommy Kinard
Comment Utility
Thanks for the Points and the Grade!

No faux pas (?) here!
dragontooth

0
 
LVL 22

Expert Comment

by:Dreamboat
Comment Utility
Dragon: That's french for screwup. LOL!

Thanks, Tristan!
0
 
LVL 1

Author Comment

by:TristanRyan
Comment Utility
Thanks for the translation, Dreamboat... I live in Montreal and forget which phrases are commonly used in English and which are understood simply because it's so maddeningly bilingual here. Example discussion between snobs: "Can you believe it? He bought dep wine for the vernissage!" "Wow... what a faux pas." (Dep wine = cheap wine, because that's what's sold in corner stores, called "dépanneurs"; vernissage = art opening).

Ah, the joys of the www.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now