How to open a Word template and save it as a Word document.

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
I am trying to assist another lister.

I thought that this would be straightforward. But. I've not worked with Word documents before. I cobbled together the code below.

VBA Code:
Sub DocFromTemplate()

    Dim sPathAndFile As String
    
    Dim oWordApp As Object
    
    Dim oWordDoc As Object

    sPathAndFile = "C:\Users\Jim\Desktop\TestTemplate.dotx"    'change to path of your file
    
    Set oWordApp = CreateObject("Word.Application")
    
    If Dir(sPathAndFile) <> "" Then     'First we check if document exists at all at given location
        Set oWordDoc = oWordApp.Documents.Open(sPathAndFile)
    End If

    With oWordDoc
        .SaveAs "C:\Users\Jim\Desktop\TestTemplate.docx"
    End With

End Sub

When I run it Excel just sits there trying to do what I asked. But it never finishes. I get this message: Excel is waiting for another application to complete an OLE action.

I gues that I need another approach?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Ok so part of the issue is known. A dialog box was present but it was behind the Excel window so I could not see it. See below.

The Word template is not open and I did not tell it to be Read Only. I surmise that Word template files Read Only, which makes sense I suppose.

How do I fix THAT? Can I tell that dialog box to open the file anyway? Read Only seems ok because I want to make a copy of it anyway.
 

Attachments

  • Screenshot 2023-06-23 125746.png
    Screenshot 2023-06-23 125746.png
    6.5 KB · Views: 4
Upvote 0
I'm making progress. I figured out how to open the Word template file so that dialog box is not shown now.

Here's the next question...After creating a copy of the Word template I want it to appear in front of Excel so user can access it. I cannot figure out how to do that? I can "Activate" it but it is still behind Excel.

VBA Code:
Sub DocFromTemplate()

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

End Sub
 
Upvote 0
Now another issue has cropped up. The file created by making a copy of the template cannot be accessed. I get the dialog below.

Oddly, after creating a copy if I go to the Word app and SaveAs the file with a new name it is openable. If I merely hit save the dialog below appears.

I'm ready to give up but there must be a way to do this so the file created is not broken and so Word is brought forward (i.e., so Word is in front of Excel).
 

Attachments

  • Dialog Word Corrupted File.png
    Dialog Word Corrupted File.png
    6.5 KB · Views: 5
Upvote 0
Hi OaklandJim. Not much luck with this one. This will bring Word to the front...
Code:
oWordApp.Visible = True
AppActivate oWordApp.ActiveWindow.Caption & " - " & oWordApp.Caption
oWordApp.WindowState = 1 ''wdWindowStateMaximize
You don't really need to open the template to copy it if you're not doing anything with it....
Code:
Dim OfsObj As Object
Set OfsObj = CreateObject("Scripting.FileSystemObject")
OfsObj.CopyFile sTargetFolder & sTemplateFileName, sTargetFolder & sNewFileName, True 'source,destination,save
Set OfsObj = Nothing
Maybe Google SaveAs2. Als, I think you need to close the template doc after saving it and then open the new doc. HTH. Dave
 
Upvote 0
I should have posted my final code. It does what was requested. I did get rid of saveas2 per your recommendation.

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
        .SaveAs sDocumentFolder & sDocumentFileName
        .Activate
    End With
    
    With oWordApp
        .Visible = True
        .Activate
        .Application.WindowState = xlNormal
    End With
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,217,011
Messages
6,134,020
Members
449,853
Latest member
kaitlynmwb

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