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(BookmarkToU pdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(B ookmarkToU pdate).Ran ge
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.A dd BookmarkToUpdate, BMRange
End Sub
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(BookmarkToU
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(B
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.A
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ActiveDocument(ClientName.
ActiveDocument(ClientName.
ActiveDocument(ClientName.
'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.
Next I
End Sub
Let me know if more is needed
dragontooth
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Tristan
Thanks for the Points and the Grade!
No faux pas (?) here!
dragontooth
No faux pas (?) here!
dragontooth
Dragon: That's french for screwup. LOL!
Thanks, Tristan!
Thanks, Tristan!
ASKER
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.
Ah, the joys of the www.
in your document itself, rather than bookmarks, you would Insert, Field, DocVariable, and name it "Client1"