Need userform content to placed in a word document

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a userform created in word 2010. It asks the user for certain information then it adds the content of the form into the blank word document. The code below is the current code. I haven't written anymore yet, until I figure out how to add it to the word document. As you can see, the document formatting is already preassigned. I just need to figure out how to add the content from the userform to the word document. Any help would be greatly appreciated, is what I am wanting to do is even possible? Thank You

Code:
Private Sub cmdbtnSubmit_Click()
    Application.Templates.LoadBuildingBlocks
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.Font.Name = "Arial"
    Selection.Font.Bold = True
    Selection.Font.Size = 48
    'Selection.PasteAndFormat (wdFormatOriginalFormatting)
    'Selection.MoveUp Unit:=wdLine, Count:=15
 
   Dim sLineSelect As String

    Select Case True
        Case Is = optSt
            sLineSelect = "St"
        Case Is = optUh2
            sLineSelect = "Uh2"
        Case Is = optKr
            sLineSelect = "Kr"
        Case Is = optIA
            sLineSelect = "IA"
        Case Is = optUh5
            sLineSelect = "Uh5"
        Case Is = optPouch
            sLineSelect = "Pouch"
    End Select

    
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The example below shows how to transfer user form information to the document.

VBA Code:
' Word UserForm module
Private Sub CommandButton1_Click()
Dim t As Table
ThisDocument.Tables.Add Selection.Range, 2, 5, wdWord9TableBehavior, wdAutoFitFixed
Set t = ThisDocument.Tables(ThisDocument.Tables.Count)
t.Cell(1, 1).Range.Text = Me.TextBox1
t.Cell(1, 2).Range.Text = Me.OptionButton1
t.Cell(1, 3).Range.Text = Me.OptionButton2
t.Cell(1, 4).Range.Text = Me.CheckBox1
End Sub
 
Upvote 0
That's truly awful code you're already working with. Contrary you what you say about "the document formatting is already preassigned", it isn't; you're merely tramping all over it. You really should learn about using Styles and Ranges for managing document content. As it is, it's not even apparent from what you've posted where it is that the userform output is to go.

One approach would be toadd a bookmark to the destination range, then use code like:
Code:
Call UpdateBookmark("BookmarkName", sLineSelect)
before your existing 'End Sub', coupled with:
Code:
Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(StrBkMk) Then
    Set BkMkRng = .Bookmarks(StrBkMk).Range
    BkMkRng.Text = StrTxt
    .Bookmarks.Add StrBkMk, BkMkRng
  End If
End With
Set BkMkRng = Nothing
End Sub
Another approach you be to use code like:
Code:
ActiveDocument.Paragraphs.Last.Range.Text = sLineSelect
 
Upvote 0
Thank You Worf. I tried the code you posted. I tried leaving my code in place and adding your code and got a runtime error '5850' "The specified range is not from the correct document or story." I even commented out all my code and just used yours and still received that same error message.
 
Upvote 0
Thank You Macropod, that worked for the first bookmark (Bookmark1) I created. I went ahead and created a total of five more bookmarks on my page.
After rereading my first post, I noticed I forgot to mention the layout of my userform. My apologies on that oversight. That being said here's the layout of the userform which includes six option buttons as shown in the first post and four textboxes.

Here is a brief summary of the bookmarks.

bookmark1= sLineSelect
bookmark2= TxtBxPrdctDescription.Value
bookmark3= txtbxCnt.Value
bookmark4= txtbxLtNum.Value
bookmark5= txtbxExpDte.Value

Thank you all for your help.
 
Upvote 0
In that case it's simply a matter of:
VBA Code:
Call UpdateBookmark("bookmark1", sLineSelect)
Call UpdateBookmark("bookmark2", TxtBxPrdctDescription.Value)
Call UpdateBookmark("bookmark3", txtbxCnt.Value)
Call UpdateBookmark("bookmark4", txtbxLtNum.Value)
Call UpdateBookmark("bookmark5", txtbxExpDte.Value)
 
Upvote 0
That was easy. So how can I either erase/clear the page when the user closes the document or erase/clear the page when the user opens the document without erasing the bookmarks.

Thank you.
 
Upvote 0
You really should be using a template to create new documents, not modifying an existing document. That way, there's little risk of the populated document overwriting anything - the new document can only be saved via Save As.

The alternative is to use the same code to clear the bookmarks when you load the userform. For example:
VBA Code:
Call UpdateBookmark("bookmark1", "")
 
Upvote 0
Ok, I have a problem. The above code that was written in Office 2007 and stored onto a flash drive and brought to work to be run on Office 365, however, Office 365 won't display user form or any of the code under the developer tab. When I took it home and ran it on my computer that I wrote it on, it works as expected. What happened? Why does Word treat VBA code differently than Excel? Thank You.
 
Upvote 0
If your Excel VBA works fine at home and at work, I can see no reason that Word VBA code that works fine at home would not also work just fine at work. Are you sure the userform and code are in the document, rather than in its template?
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top