Option Explicit
Sub CreateWordDocFromTemplate()
Dim sDocName As String
Dim sTemplateName As String
Dim sDocPath As String
Dim sTemplatePath As String
sDocName = Format(Now, "yyyy-mm-dd hh-mm-ss") & ".docx"
sTemplateName = "TestTemplate.dotx"
sDocPath = Environ("UserProfile") & "\Desktop\"
sTemplatePath = Environ("UserProfile") & "\Desktop\"
Call DoCreateWordDoc(sDocName, sTemplateName, sDocPath, sTemplatePath)
End Sub
'Requires a Reference to Microsoft Word 16.0 Word Object Library
'In VBA editor select Tools => References then find the checkbox for
'Microsoft Word 16.0 Word Object Library and check it (on).
Sub DoCreateWordDoc( _
psDocName As String, _
psTemplateName As String, _
psDocPath As String, _
psTemplatePath)
Dim wdApp As Word.Application
Dim sSaveAsName As String
Set wdApp = New Word.Application
With wdApp
.Documents.Add psTemplatePath & psTemplateName
.ActiveDocument.SaveAs2 psDocPath & psDocName
.Visible = True
.Activate
.Application.WindowState = xlNormal
End With
End Sub