Word documents from Access

S

Sandy

I am printing Word 2000 letters and envelopes from an
Access 97 database using Windows NT. To avoid leaving an
instance of Winword running each time I print, I tried
adding "objWord.Quit" to the following code. However, I
keep getting a message that: Word is currently printing,
quitting Word will cancel the print job, do I want to quit
Word? If I say 'yes' the print job is cancelled. If I
say 'no' Winword is left open. Any help would be greatly
appreciated. Thank you.

Sub PrtLetter()
Dim objWord As Object

' Open Microsoft Word using automation
Set objWord = New Word.Application
objWord.Documents.Add gstrTxLetterName, acNormal
objWord.Visible = False

'Pass data from Access to Word document
If objWord.ActiveDocument.Bookmarks.Exists
("PatientName") = True Then
objWord.ActiveDocument.Bookmarks
("PatientName").Range.Text = Me![FirstName] & " " &
[LastName]
End If

'Print the envelope
objWord.ActiveDocument.Envelope.PrintOut
ExtractAddress:=True, OmitReturnAddress _
:=True, PrintBarCode:=False, PrintFIMA:=False,
Height:=InchesToPoints( _
4.13), Width:=InchesToPoints(9.5)

'Print the letter and close Word
objWord.ActiveDocument.PrintOut
objWord.ActiveDocument.Close wdDoNotSaveChanges
objWord.Quit
Set objWord = Nothing

End Sub
 
J

John Nurick

Hi Sandy,

IIRC you can avoid this by ensuring that Word's "background printing"
option is turned off" with
objWord.Options.PrintBackground = False

If that's not acceptable, try using a loop or a form's timer to wait
until objWord.BackgroundPrintingStatus = 0 before closing the document
or quitting Word.

I am printing Word 2000 letters and envelopes from an
Access 97 database using Windows NT. To avoid leaving an
instance of Winword running each time I print, I tried
adding "objWord.Quit" to the following code. However, I
keep getting a message that: Word is currently printing,
quitting Word will cancel the print job, do I want to quit
Word? If I say 'yes' the print job is cancelled. If I
say 'no' Winword is left open. Any help would be greatly
appreciated. Thank you.

Sub PrtLetter()
Dim objWord As Object

' Open Microsoft Word using automation
Set objWord = New Word.Application
objWord.Documents.Add gstrTxLetterName, acNormal
objWord.Visible = False

'Pass data from Access to Word document
If objWord.ActiveDocument.Bookmarks.Exists
("PatientName") = True Then
objWord.ActiveDocument.Bookmarks
("PatientName").Range.Text = Me![FirstName] & " " &
[LastName]
End If

'Print the envelope
objWord.ActiveDocument.Envelope.PrintOut
ExtractAddress:=True, OmitReturnAddress _
:=True, PrintBarCode:=False, PrintFIMA:=False,
Height:=InchesToPoints( _
4.13), Width:=InchesToPoints(9.5)

'Print the letter and close Word
objWord.ActiveDocument.PrintOut
objWord.ActiveDocument.Close wdDoNotSaveChanges
objWord.Quit
Set objWord = Nothing

End Sub
 
S

Sandy

John,

Thank you so much. This morning I added the line of code
to ensure background printing for Word is turned off and
it seems to be working beautifully!

Sandy
 
S

Sandy

I may have spoken too soon this morning regarding the
printing of Word 2000 letters and envelopes successfully
from an Access 97 database using Windows NT. After
ensuring that Word's background printing option was turned
off, I was able to print several letters and envelopes
before eventually repeatedly getting the following error:

Run-time erro '-2147023174 (800706ba)':

Automation error
The RPC server is unavailable.

I only get this error if I try to print the envelopes and
the letters. If I do not print the envelopes, I do not get
the error. Thank you for your help.
 
J

John Nurick

This sounds as if it may be related to the problem described at
http://support.microsoft.com/?id=188546

If you're printing multiple letters and envelopes I'd recommend creating
one instance of Word, printing the whole batch, and only then quitting.
IOW instead of calling your present PrtLetter() sub multiple times, put
the code that creates and detroys the instance of Word into the calling
code. Then pass Word to the printing code like this:

Public Sub PrtLetterWithWord( _
LetterName as String, _
objWord As Word.Application )

With objWord
.Documents.Add gstrTxLetterName, acNormal

'Pass data from Access to Word document
With ActiveDocument.Bookmarks
If .Exists("PatientName") Then
.Item("PatientName").Range.Text = _
Me![FirstName] & " " & [LastName]
End If
End With

'Print the envelope
.ActiveDocument.Envelope.PrintOut _
ExtractAddress:=True,
OmitReturnAddress:=True, _
PrintBarCode:=False, _
PrintFIMA:=False, _
Height:=InchesToPoints(4.13), _
Width:=InchesToPoints(9.5)

'Print the letter and close Word
.ActiveDocument.PrintOut

'Close all documents (it's conceivable that
'a macro or add-in has opened one automatically)
Do While .Documents.Count > 0
.Documents(1).Close wdDoNotSaveChanges
Loop
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top