PDA

View Full Version : SaveAs NewTitle from Template



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

Tinbendr
09-13-2010, 07:37 PM
Any ideas?Try recording the manual SaveAs and see what other settings Word is using to save the document. Then add them to your VBA SaveAs until you pin point it.

Good Luck!

Imdabaum
09-14-2010, 09:45 AM
*Office 2007

Sub SaveAsmacro()
Attribute SaveAsmacro.VB_ProcData.VB_Invoke_Func = "TemplateProject.NewMacros.SaveAsmacro"
'
' SaveAsmacro Macro
'
'
ChangeFileOpenDirectory _
"C:\Documents and Settings\bbaum\My Documents\AccessProjects\"
ActiveDocument.SaveAs FileName:="FileRequest197.docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub

I don't get half of these options from Access though. The SaveAs only has one parameter, the destination file.