Link to home
Start Free TrialLog in
Avatar of TristanRyan
TristanRyan

asked on

VBA function: loop to replace bookmarks with VBA TextBox value

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
SOLUTION
Avatar of Joanne M. Orzech
Joanne M. Orzech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops...sorry - to elaborate

in your document itself, rather than bookmarks, you would Insert, Field, DocVariable, and name it "Client1"
Avatar of Tommy Kinard
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


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TristanRyan
TristanRyan

ASKER

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
Thanks for the Points and the Grade!

No faux pas (?) here!
dragontooth

Dragon: That's french for screwup. LOL!

Thanks, Tristan!
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.