C
cmw
I've created a letterwriting application which opens word, prefills a
template with data from Access, prints the word document, saves the document,
then closes it. I've cloned the code in several different routines. For some
reason the latest routine opens word and prints the letter the first time,
but in subsequent calls does not recognize the open word application
(although other routines do).
I found that if I don't save the document, it does not close without user
intervention, so I save the document then close it after it is printed. I
then set the word object to nothing. Word remains open and in all but the
last routine, the next time it is invoked, the same word session is opened.
Code:
Dim WordObj As Word.Application
Dim TemplateFile As String
dim SaveDoc as string
TemplateFile = "c:\SampleTemp.dot"
Set WordObj = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordObj = CreateObject("Word.Application")
End If
WordObj.Visible = True
WordObj.Documents.Add Template:=TemplateFile, NewTemplate:=False
'Fill word document with data from Access
DoEvents
WordObj.Activate
ActiveDocument.PrintOut
SaveDoc = "c:\PrintedLetter.doc" 'In some routines, the same file is
overwritter
'in others, the file is
saved with a unique name
'this routine overwrites
one file
ActiveDocument.SaveAs FileName:=SaveDoc, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveDocument.Close
Set WordObj = Nothing
template with data from Access, prints the word document, saves the document,
then closes it. I've cloned the code in several different routines. For some
reason the latest routine opens word and prints the letter the first time,
but in subsequent calls does not recognize the open word application
(although other routines do).
I found that if I don't save the document, it does not close without user
intervention, so I save the document then close it after it is printed. I
then set the word object to nothing. Word remains open and in all but the
last routine, the next time it is invoked, the same word session is opened.
Code:
Dim WordObj As Word.Application
Dim TemplateFile As String
dim SaveDoc as string
TemplateFile = "c:\SampleTemp.dot"
Set WordObj = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordObj = CreateObject("Word.Application")
End If
WordObj.Visible = True
WordObj.Documents.Add Template:=TemplateFile, NewTemplate:=False
'Fill word document with data from Access
DoEvents
WordObj.Activate
ActiveDocument.PrintOut
SaveDoc = "c:\PrintedLetter.doc" 'In some routines, the same file is
overwritter
'in others, the file is
saved with a unique name
'this routine overwrites
one file
ActiveDocument.SaveAs FileName:=SaveDoc, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveDocument.Close
Set WordObj = Nothing