Log in

View Full Version : VBA to Move Data from UserForm to Bookmarks on Document



Kryptoooo
05-08-2015, 02:53 AM
Hello, trying to make it so if I click on the "ok" button it takes all of the information I have written out in the textboxes, and put them on the document in the correct bookmark spot

I have tried a few methods here and there, although cannot get them working...

textbox name: txtFirstName
bookmark name: firstName

not sure if more information is needed - there are more textboxes / bookmarks although from one example I can easily fill in the rest.


Thanks a lot
Kryptooooo

gmayor
05-08-2015, 04:17 AM
You will find the FillBM function at http://www.gmayor.com/word_vba_examples.htm
Call this from your useform code as many times as you have bookmarks and fields, to fill your bookmarks with the textbox texts. The only proviso is that the bookmarks must not overlap.

e.g.
FillBM "firstName", Me.txtFirstName.Text

etc

Kryptoooo
05-08-2015, 04:36 AM
I don't know exactly where to put it, but I pasted it at the bottom of my Module containing all of the different button codes etc. and the first thing it did was return with an error
"Compile Error: Invalid use of Me keyword" for the Me. parts.

Thanks for the help.

gmaxey
05-08-2015, 05:10 AM
The FillBM procedure can go in the UserForm module or any standard module. The call i.e., FillBM "firstName", Me.txtFirstName.Text is a line of code you would put in the command button click event procedure in the Userform module. "Me" refers to the userform object.

gmayor
05-08-2015, 05:15 AM
See - http://www.gmayor.com/Userform.htm

Kryptoooo
05-08-2015, 05:53 PM
Thanks still had issues with the whole Me. thing sadly but I renamed them all with my UserForm name instead, now the only issue that I can see is it highlights "FillBM" and says
"Compile Error: Sub or Function not defined"

All my code is in a module called buttonMacros, but the FillBM Sub is in "ThisDocument" it is set as Public Sub, although it supposibly cannot be found.



Public Sub FillBM(strBMName As String, strValue As String)
Dim oRng As Range
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = strValue
oRng.Bookmarks.Add strBMName
End With

lbl_Exit:
Exit Sub
End Sub


And this is the part where I am calling the FillBM:



Sub mcrOk()

frmParticipantInput.Hide

FillBM "FirstandLastName", frmParticipantInput.txtFirstName.Text
FillBM "firstName", frmParticipantInput.txtFirstName.Text
FillBM "lastName", frmParticipantInput.txtLastName.Text

Unload frmParticipantInput

End Sub

gmaxey
05-08-2015, 06:21 PM
Why did you put FillBM in ThisDocument? ThisDocumet is a special class object. Put it in the same standard module as your mcrOk procedure or another standard module.

Kryptoooo
05-08-2015, 07:38 PM
Ah, thanks - sorry don't know what I was thinking havn't messed with Word VBA + ThisDocument in a while, it works perfectly though thanks.