Need userform content to placed in a word document

Pookiemeister

Board Regular
Joined
Nov 26, 2015
Messages
221
Office Version
365, 2010
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
 

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
3,825
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,323
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
 

Pookiemeister

Board Regular
Joined
Nov 26, 2015
Messages
221
Office Version
365, 2010
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.
 

Pookiemeister

Board Regular
Joined
Nov 26, 2015
Messages
221
Office Version
365, 2010
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.
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,323
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)
 

Pookiemeister

Board Regular
Joined
Nov 26, 2015
Messages
221
Office Version
365, 2010
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.
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,323
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", "")
 

Pookiemeister

Board Regular
Joined
Nov 26, 2015
Messages
221
Office Version
365, 2010
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.
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,323
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?
 

Forum statistics

Threads
1,085,785
Messages
5,385,863
Members
401,975
Latest member
OnPoint

Some videos you may like

This Week's Hot Topics

Top