Consulting

Results 1 to 3 of 3

Thread: Userform to open automatically when saved document opened

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location

    Userform to open automatically when saved document opened

    Hi,

    I'm hoping someone can help with a problem that I have...

    I have created a word template that makes use of a userform to populate areas of a document (doc variables). It all works exactly as I want it to, at least up to the point where the user saves the document and then reopens it at a later date, probably to make changes to the info in the userform.

    I have managed to get the saved info from the document back into the form but what I can't figure out is how to make the (already filled-in) userform appear immediately and automatically when the saved macro-enabled word doc is opened. If I run the CallUF code manually, it all works okay, but I need this to be automated, with the opening of the word doc being the trigger.

    I have relied heavily on Greg Maxey's site for the code that I have used (thank you Greg if you see this), but can't figure out this last stage so thanks for any help you can give.


    Abbreviated code that I have is:

    (Userform)

    Private Sub Userform_Initialize()
    Dim arrString() As String
    With Me
    With .CmbCDur
    .AddItem "One Year"
    .AddItem "Two Years"
    .AddItem "Three Years Fixed Price"
    .AddItem "Five Years Fixed Price"
    .AddItem "One-Off Maintenance"
    End With

    ' Load properties into text boxes
    On Error Resume Next
    Me.txtDate = ActiveDocument.Variables("varDate")
    Me.txtRef = ActiveDocument.Variables("varRef")
    End With
    lbl_Exit:
    Exit Sub
    End Sub

    (Module)


    Sub CallUF()
    Dim oFrm As FrmDetails
    Dim pStr As String
    Dim oRng As Word.Range
    Dim i As Long
    Dim pMulSel As String
    Set oVars = ActiveDocument.Variables
    Set oFrm = New FrmDetails
    With oFrm
    .Show
    oVars("varDate").Value = .txtDate.Text
    oVars("varRef").Value = .txtRef.Text

    'Process responses (including no response)

    If .CmbCDur.Value <> "" Then
    oVars("varCDur").Value = .CmbCDur.Value
    Else: oVars("varCDur").Value = "No response"
    End If
    UpdateThisFormsFields
    End With


    Unload oFrm
    Set oFrm = Nothing
    Set oVars = Nothing
    Set oRng = Nothing
    End Sub


    Sub AutoNew()
    Create_Reset_Variables
    CallUF
    End Sub


    Sub UpdateThisFormsFields()
    Dim pRange As Word.Range
    Dim iLink As Long
    iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    For Each pRange In ActiveDocument.StoryRanges
    Do
    pRange.Fields.Update
    Set pRange = pRange.NextStoryRange
    Loop Until pRange Is Nothing
    Next
    End Sub
    Sub Create_Reset_Variables()
    With ActiveDocument.Variables
    .Item("varDate").Value = " "
    .Item("varRef").Value = " "


    End With


    End Sub

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub AutoOpen
    CallUF
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Posts
    17
    Location
    That's great, thank you for your help. It now works perfectly.


    Anne

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •