PDA

View Full Version : [SOLVED:] Userform to open automatically when saved document opened



AnnieM
09-16-2016, 12:07 PM
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

gmaxey
09-16-2016, 04:05 PM
Sub AutoOpen
CallUF
End Sub

AnnieM
09-16-2016, 04:41 PM
That's great, thank you for your help. It now works perfectly.


Anne