PDA

View Full Version : Open Word Template, and have THAT file's Userform open



klg
02-14-2019, 10:33 AM
Hi there,

I have a main excel document that gathers data, it has a command button that opens a Word Letter template. That word document has a userform that "should" open when that word template opens. Is there a way I can open the word document AND it's userform when that word document opens?
This is the code in my excel file that open's the word template


Private Sub CommandButton11_Click()
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Open Filename:="C:\Users\Documents\Custom Office Templates\Letter 0219.dotm"
End Sub

Here is the code in Word template that opens the Userform when the document is open


Option Explicit
Dim oFrm As Userform1
Sub AutoNew()
Set oFrm = New Userform1
With oFrm
.Show
If .Tag = "CREATE DOC" Then
FillDoc
Else
ActiveDocument.Close wdDoNotSaveChanges
End If
End With
lbl_Exit:
Exit Sub
End Sub

gmayor
02-21-2019, 01:48 AM
This question seems to have been overlooked, but the issue you have here is that the template is being opened rather than a new document created from it, so changing the following line as shown should cause the autonew macro to fire (or if you really want to open the template, add an AutoOpen macro to the template to fire when the template is opened.

Or you could add the line

appWD.Run ("AutoNew") to the first of the following:

Change

appWD.Documents.Open Filename:="C:\Users\Documents\Custom Office Templates\Letter 0219.dotm"
to

appWD.Documents.Add Template:="C:\Users\Documents\Custom Office Templates\Letter 0219.dotm"

klg
02-21-2019, 08:16 AM
Perfection! thank-you so much!!!