Creating a word document from within excel

StephenBart

New Member
Joined
Jun 22, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I want to launch a word template from within excel. I can create a hyperlink to the .dotx file no problem. But that opens the template. I am trying to achieve the result where a .doc file version based on the word template opens. Any suggestions?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I believe that the code below does what you need. It opens a copy of the template file based on the template and allows user to edit that new file.

As always for me, other experts write more elegant, faster code. Maybe someone will chime in with a better approach.

Good Luck!

VBA Code:
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
 
Upvote 0
I did put together a version that does not require any references. For completeness...

VBA Code:
Option Explicit

'Does not require setting reference to the Microsoft Word 16.0 Word Object Library.

Sub DocFromTemplate()

    Dim oWordApp As Object
    
    Dim oWordDoc As Object
    
    Dim sTemplateFolder As String
    
    Dim sTemplateFileName As String
    
    Dim sDocumentFolder As String
    
    Dim sDocumentFileName As String
    
    sTemplateFileName = "TestTemplate.dotx"
    
    sTemplateFolder = Environ("UserProfile") & "\Desktop\"
    
    sDocumentFileName = "CopyOfTemplate.docx"
    
    sDocumentFolder = Environ("UserProfile") & "\Desktop\"
    
    Set oWordApp = CreateObject("Word.Application")
    
    On Error Resume Next
    Kill sDocumentFolder & sDocumentFileName
    On Error GoTo 0
    
    If Dir(sTemplateFolder & sTemplateFileName) <> "" Then     'Check if document exists at given location
        Set oWordDoc = oWordApp.Documents.Open(sTemplateFolder & sTemplateFileName, ReadOnly:=True)
    Else
        MsgBox "The Word template file " & sTemplateFileName & " was not found in" _
               & Chr(10) & sTemplateFolder, vbInformation
        Exit Sub
    End If
    
    With oWordDoc
        .SaveAs2 sDocumentFolder & sDocumentFileName
        .Activate
    End With
    
    With oWordApp
        .Visible = True
        .Activate
        .Application.WindowState = xlNormal
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,135
Messages
6,123,241
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top