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.
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