• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

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
0
MikeXna
Asked:
MikeXna
1 Solution
 
GrahamSkanRetiredCommented:
I have put the routine to insert the text into a Sub of its own, since it now needs to be done more than once,
Private Sub CommandButton1_Click()
    Dim sChosenValue As String
    
    ' 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
    
    InsertTextIntoBookmark "Bookmark1", sChosenValue
    
    InsertTextIntoBookmark "Bookmark2", TextBox1.Text
    ' Remove the form
    Unload UserForm1
End Sub

Sub InsertTextIntoBookmark(strName As String, strText As String)
    Dim bmRange As Range
    
    Set bmRange = ActiveDocument.Bookmarks(strName).Range
    bmRange.Text = strText
    're-add bookmark to ensure that it contains the new text
    ActiveDocument.Bookmarks.Add strName, bmRange
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now