Word crashing when printer tray is changed in vba

G

Graham Mayor

For some time now I have had some printer switching routines detailed on my
web site http://www.gmayor.com/fax_from_word.htm and they have always worked
in the past ... until recently when I responded to a question in the mail
merge group to a user who found that Word crashed when the tray options were
selected. The following sample code should show the problem (change the
printer name to a valid printer that is not the default printer and which
has a tray 2 assignment available:

Sub HPPrint()
Dim sPrinter As String
Dim sTray As String
sTray = Options.DefaultTray
With Dialogs(wdDialogFilePrintSetup)
sPrinter = .Printer
.Printer = "HP LaserJet 4050 Series PCL"
.DoNotSetAsSysDefault = True
.Execute
End With
Options.DefaultTray = "Tray2"
MsgBox ActivePrinter & vbCr & Options.DefaultTray
'Dialogs(wdDialogFilePrint).Show
With Dialogs(wdDialogFilePrintSetup)
.Printer = sPrinter
.DoNotSetAsSysDefault = False
.Execute
End With
Options.DefaultTray = sTray
MsgBox ActivePrinter & vbCr & Options.DefaultTray
End Sub

I find this invariably crashes Word 2003 (sp2 and sp3) and in Word 2007 the
line
Options.DefaultTray = "Tray2"
is ignored


Even using the simpler

Sub HPPrint()
Dim sPrinter As String
Dim sTray As String
sTray = Options.DefaultTray
sPrinter = ActivePrinter
ActivePrinter = "HP LaserJet 4050 Series PCL"
Options.DefaultTray = "Tray2"
MsgBox ActivePrinter & vbCr & Options.DefaultTray
ActivePrinter = sPrinter
Options.DefaultTray = sTray
MsgBox ActivePrinter & vbCr & Options.DefaultTray
End Sub

the same results apply?

I had suspected Acrobat may have been the problem, but I uninstalled that
and the problem remains.
Is anyone else experiencing this problem?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

Hi Graham

I've done some experimenting, and my conclusion is that you get a crash in
Word 2003 if you try to assign a non-existent tray name to
Options.DefaultTray.

If you assign a non-existent tray ID number to Options.DefaultTrayID, then
Word simply ignores the assignment and keeps the existing tray assignment,
whatever it is.

I experimented using the HP LaserJet 4250 PCL 5e driver, and found that
"Tray2" and "Tray 2" both don't exist, and therefore cause a crash, but "
Tray 2" (with a leading space) does exist and works OK.

For safely, I would be inclined to work exclusively with Tray ID codes
rather than tray names. As it happens, the FirstPageTray and OtherPagesTray
properties of the PageSetup object will only take tray IDs, not names.

This looks very much like a bug in Word 2003 which has been fixed in 2007.
It is possible that it is unique to HP printers and is in fact a bug in the
HP drivers which just react badly to Word 2003 for some reason. I haven't
experimented with any non-HP printers.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 
G

Graham Mayor

That works for me too :) Thanks.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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