VBA - Save Word Document Template as New Word Document

PHayes35

New Member
Joined
Dec 6, 2017
Messages
6
Hey guys, so I have the below code that takes named ranges in Excel and updates them in a word document template I have set up with bookmarks. I'd like to save this word document template as a newly named file, referencing a range on the excel worksheet I'm running the code on.

Below is my current code, I'm receiving the "object doesn't support this property or method" error on the code in red. Filepath/filename have been renamed for proprietary reasons.

Code:
Dim wb As Workbook
    Dim pappWord As Object, docWord As Object
    Dim FlName As String
    Dim xlName As Name
    Dim filePath As String
        
 filePath = "\\CompanyAddress.com\bc1\Files\XXX_Common\XXX\XXXX XX\XXXXXX\Patrick\BET\Term Sheets" 
    
Sheets("Basket Template").Select
    
 FlName = "\\CompanyAddress.com\bc1\Files\XXX_Common\XXX\XXXX XX\XXXXXX\Patrick\BET\Basket Template 1.docm"
 
 
  On Error Resume Next
    Set pappWord = GetObject(, "Word.Application")
       
       
       If Err.Number <> 0 Then
        Set pappWord = CreateObject("Word.Application")
    End If
    Err.Clear
    On Error GoTo 0


   Set docWord = pappWord.Documents.Open(FlName)
   
   Set wb = ActiveWorkbook
   
   Sheets("Basket Template").Select
   
   
    For Each xlName In wb.Names
         'if xlName's name is existing in document then put the value in place of the bookmark
        If docWord.Bookmarks.Exists(xlName.Name) Then
            docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName).Value
        End If
    Next xlName
    
    
      With pappWord
        .Visible = True
        .ActiveWindow.WindowState = 0
        .Activate
[COLOR=#ff0000]        .SaveAs filePath & "\" & Sheets("Basket Template").Range("C2").Value & ".docm"[/COLOR]


        
    End With
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Code:
With docWord
 .SaveAs filePath & "\" & CSTR(Sheets("Basket Template").Range("C2").Value) & ".docm"
End With
HTH. Dave
 
Last edited:
Upvote 0
I note that you've referred to the Word file you're using a template but it is not, in fact, a Word template. Word templates have extensions like .dot, .dotx or .dotm; yours is a macro-enabled Word document. Furthermore, you'd create a new document from a template using
Set docWord = pappWord.Documents.Add(FlName)
not:
Set docWord = pappWord.Documents.Open(FlName)

As for your SaveAs problem, SaveAs is not a Word application method, its w Word document method. Hence you should have:
docWord.SaveAs2 filePath & "" & Sheets("Basket Template").Range("C2").Value & ".docm", 13 'wdFormatXMLDocumentMacroEnabled
or:
docWord.SaveAs2 filePath & "" & Sheets("Basket Template").Range("C2").Value & ".docx", 12 'wdFormatXMLDocument

There is also no need for:
Sheets("Basket Template").Select
or:
.ActiveWindow.WindowState = 0
.Activate
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,537
Members
452,571
Latest member
MarExcelTips

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