Link to home
Start Free TrialLog in
Avatar of MikeXna
MikeXna

asked on

MS Word and VB - Help needed sending TextBox value in a VB form to a Bookmark location in a preformatted .docm document template

Hello Experts,

I am newbie to using VB and Word together to create a professional document generation template.  I need some help putting 2 and 2 together in the attached "Hello World" sample that upon opening the document it opens to a vb form for the user to fill out and sends the field values to predifined bookmarks in the formatted Word document.
 
I am trying to add to the attached samlpe (rename zip to docm) which currently opens to the form and inserts some different text to a bookmark loacation, depending on what option button was selected. It works thanks to a helpful expert!

I recently added a second bookmark called "bookmark2" in the document and added a texbox in my form called TextBox1.

I need some help adding the correct VB code in the buttons Click event which will send the value of what I type in that textbox to my second bookmark.

I am little confused of how and when to declare variables as I am more of a help desk/web guy than a VB programmer.   If you share code, can you please add as many comments as possible to help me understand the basics of VB?

I found this older post: http://www.eggheadcafe.com/software/aspnet/32193941/userform-question.aspx but my attempt at following advice produces a form that does nothing after clicking the button. =(
If possible can you post a working sample similar to this one with multiple text box entries?  I would really appreciate it!

Thanks Again!
Private Sub CommandButton1_Click()
Dim sChosenValue As String
Dim bmRange As Range

' Find out what the user has chosen
If Me.OptionButton1.Value = True Then
    sChosenValue = "First option chosen!"
ElseIf Me.OptionButton2.Value = True Then
    sChosenValue = "Second option chosen!"
ElseIf Me.OptionButton3.Value = True Then
    sChosenValue = "Third option chosen!"
End If

' set bmRange to the bookmark in the document you want the text to be insertet into
Set bmRange = ActiveDocument.Bookmarks("Bookmark1").Range

' Insert the text
bmRange.Text = sChosenValue

' re-set the bookmark
ActiveDocument.Bookmarks.Add _
   Name:="Bookmark1", _
   Range:=bmRange

' Remove the form
Unload UserForm1

End Sub

Open in new window

Insert-Text-Bookmark.zip
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland 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