Hello,
I am having trouble saving (naming) a new word doc dependant upon what a cell value is. The folder name where the word doc should be saved is also based on a certain cell value.
I keep receiving an error: Error number 438. There is a problem
Below is the code:
Thanks, Oreg
Option Explicit
Sub BCMerge()
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
'Dim TodayDate As String
Dim ActiveDocument As Object
Dim Path As String
Set wb = ActiveWorkbook
'TodayDate = Format(Date, "mmmm d, yyyy")
Path = wb.Path & "\MetroTemplate.dot"
On Error GoTo ErrorHandler
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
On Error GoTo ErrorHandler
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
'Loop through names in the activeworkbook
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
'Activate word and display document
ChDir "C:\Automated Metro\Metro PCI Cover Page\" & wb.Range("C5")
If Err.Number = 76 Then MkDir "C:\Automated Metro\Metro PCI Cover Page\" & wb.Range("C5")
pappWord.SaveAs Filename:="C:\Automated Metro\Metro PCI Cover Page\" & wb.Range("C5") & "\" & _
wb.Range("C5") & "_" & wb.Range("D7") & ".doc"
With pappWord
.Visible = True
.ActiveWindow.WindowState = 1
.Activate
End With
'Next
Application.DisplayAlerts = True
'Release the Word object to save memory and exit macro
ErrorExit:
Set pappWord = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not pappWord Is Nothing Then
pappWord.Quit False
End If
Resume ErrorExit
End If
With wb
Application.IgnoreRemoteRequests = False
wb.Close True
End With
End Sub
Bookmarks