none
Inserting data from UserForm into Word 2010 Template RRS feed

  • Question

  • Hi

    I am having difficulty getting the text from the user form into my Word document. So far, I have done the following:

    • Created a Word Macro-enabled template containing DocVariable fields and a bookmark for text I wish to have repeated in the document;
    • Created a UserForm (and coding) called frmAssessment and a module called modAssessement in the Template Project.

    As I am new to this I have found Greg Maxey's information on Employing a User Form very valuable and have used much (ok, almost all) of his code as my boilerplate. I am still getting my head around which code belongs in the Forms and which code belongs in the Modules. This is what I have (apologies in advance if it looks like a dog's breakfast:

    Forms code:

    Option Explicit
    Public boolProceed As Boolean
    Private Sub cmdBtnOK_Click()
      Select Case ""
        Case Me.txtQ1.Value
          MsgBox "Please answer the question."
          Me.txtQ1.SetFocus
          Exit Sub
        Case Me.txtQ2.Value
          MsgBox "ANSWER THE QUESTION!"
          Me.txtQ2.SetFocus
          Exit Sub
        Case Me.txtQ3.Value
          MsgBox "Are you serious?"
          Me.txtQ3.SetFocus
          Exit Sub
      End Select
      Me.boolProceed = True
      Me.Hide
    lbl_Exit:
      Exit Sub
    End Sub

    Private Sub cmdCancel_Click()
    Me.Hide
    End Sub

    Modules code:

    Sub CallUF()
    Dim oFrm As frmAssessment
    Dim oVars As Word.Variables
    Dim strTemp As String
    Dim oRng As Word.Range
    Dim i As Long
    Dim strMultiSel As String
        Set oVars = ActiveDocument.Variables
        Set oFrm = New frmAssessment
        With oFrm
          .Show
      If .boolProceed Then
        oVars("varQ1").Value = .txtQ1
        oVars("varQ2").Value = .txtQ2
        oVars("varQ3").Value = .txtQ3
    Set oRng = ActiveDocument.Bookmarks("bmQ1").Range
        oRng.Text = strTemp
        ActiveDocument.Bookmarks.Add "bmQ1", oRng
    UpdateThisFormsFields
      Else
        MsgBox "Form cancelled by user"
      End If
    End With
    Unload oFrm
    Set oFrm = Nothing
    Set oVars = Nothing
    Set oRng = Nothing
    lbl_Exit:
        Exit Sub
    End Sub

    Sub AutoNew()
    Create_Reset_Variables
    CallUF
    End Sub

    Sub UpdateThisFormsFields()
    Dim oRng As Word.Range
    Dim iLink As Long
    iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    For Each oRng In ActiveDocument.StoryRanges
      Do
        oRng.Fields.Update
        Set oRng = oRng.NextStoryRange
      Loop Until oRng Is Nothing
    Next
    End Sub
    Sub Create_Reset_Variables()
    With ActiveDocument.Variables
      .Item("varQ1").Value = " "
      .Item("varQ2").Value = " "
      '.Item("varQ1").Value = " "
     
    End With
    myUpdateFields
    lbl_Exit:
        Exit Sub
    End Sub

    Many thanks for any help received.

    Kind regards

    Michelle

    Friday, July 6, 2012 5:43 AM