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!
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!"
' 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
' Remove the form