[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-01
1
Medium Priority
?
467 Views
Last Modified: 2012-05-11
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
Comment
Question by:MikeXna
1 Comment
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 35503028
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question