Imdabaum
09-13-2010, 03:39 PM
I have the following code that opens a template file for mail merge with data from Access. I want to then save the file as a new name. If I manually perform the save as. It works. But if I open it after doing it through VBA, it tells me there is a problem with the contents of the document. Any ideas?
Function OpenMergedDoc(strDocName As String, strMergedDocName As String) As Boolean
On Error GoTo WordError
'************************************************************************** *****************
Dim objWord As New Word.Application
Dim objDoc As Word.Document
Dim strDir as String
strDir = "C:\"
objWord.Application.Visible = True
Set objDoc = objWord.Documents.Open(strDir & strDocName)
' Make Word visible so that if any errors occur,
' you can close the instance of Word manually
objWord.Application.Visible = True
'*optional code to merge to a new document, save the merged document, and close the template goes here*
objDoc.SaveAs (strDir & strMergedDocName)
Exit_MailMerged:
'release the variables
objWord.Quit
Set objWord = Nothing
Set objDoc = Nothing
OpenMergedDoc = True
Exit Function
WordError:
MsgBox "Err #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Word Error"
OpenMergedDoc = False
objWord.Quit
End Function
Function OpenMergedDoc(strDocName As String, strMergedDocName As String) As Boolean
On Error GoTo WordError
'************************************************************************** *****************
Dim objWord As New Word.Application
Dim objDoc As Word.Document
Dim strDir as String
strDir = "C:\"
objWord.Application.Visible = True
Set objDoc = objWord.Documents.Open(strDir & strDocName)
' Make Word visible so that if any errors occur,
' you can close the instance of Word manually
objWord.Application.Visible = True
'*optional code to merge to a new document, save the merged document, and close the template goes here*
objDoc.SaveAs (strDir & strMergedDocName)
Exit_MailMerged:
'release the variables
objWord.Quit
Set objWord = Nothing
Set objDoc = Nothing
OpenMergedDoc = True
Exit Function
WordError:
MsgBox "Err #" & Err.Number & " occurred." & Err.Description, vbOKOnly, "Word Error"
OpenMergedDoc = False
objWord.Quit
End Function