Hypercat (Deb)
asked on
Visual Basic error running macro: Runtime error 4608 "Value out of range"
We have a Word XP macro that selects a particular printer, sets the printers trays, prints the document then reset the original default printer. Works great 95% of the time. On occasion on certain documents we get a runtime error of 4608 "Value out of range" and find that VB has stuck on ".FirstPageTray = wdPrinterAutomaticSheetFee d". This problem recently came up in a document and in my testing I removed the one an only "section break next page" and the macro worked. But testing another similar document the macro worked even with a "section break next page" in place?? What is causing this runtime error. If it is section breaks how can I get the macro to work around/with them?
Below is the text of the macro
Sub PlainPrint_B()
'
' PlainPrint-B Macro
' Prints to Aficio 2075-B, Plain paper
'
' Get active/default printer.
strActivePrinter = Application.ActivePrinter
'Change the active printer to match local Aficio 2075-B
ActivePrinter = "\\circe\Aficio 2075-B PCL6"
With ActiveDocument.PageSetup
'Auto Select tray
.FirstPageTray = wdPrinterAutomaticSheetFee d
'Auto Select tray
.OtherPagesTray = wdPrinterAutomaticSheetFee d
End With
Application.PrintOut
' Change back to the default printer.
Application.ActivePrinter = strActivePrinter
End Sub
Below is the text of the macro
Sub PlainPrint_B()
'
' PlainPrint-B Macro
' Prints to Aficio 2075-B, Plain paper
'
' Get active/default printer.
strActivePrinter = Application.ActivePrinter
'Change the active printer to match local Aficio 2075-B
ActivePrinter = "\\circe\Aficio 2075-B PCL6"
With ActiveDocument.PageSetup
'Auto Select tray
.FirstPageTray = wdPrinterAutomaticSheetFee
'Auto Select tray
.OtherPagesTray = wdPrinterAutomaticSheetFee
End With
Application.PrintOut
' Change back to the default printer.
Application.ActivePrinter = strActivePrinter
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There is something of a problem in choosing the tray number. Many of the printer drivers do not use numbers from the wdPaperTray enumeration. The code would look something like this:
Sub PlainPrint_B()
Dim sec As Section
'
' PlainPrint-B Macro
' Prints to Aficio 2075-B, Plain paper
'
' Get active/default printer.
strActivePrinter = Application.ActivePrinter
'Change the active printer to match local Aficio 2075-B
ActivePrinter = "\\circe\Aficio 2075-B PCL6"
For Each sec In ActiveDocument.Sections
With sec.PageSetup
If sec.Index = 1 Then
.FirstPageTray = wdPrinterUpperBin
.OtherPagesTray = wdPrinterAutomaticSheetFee d
Else
.FirstPageTray = wdPrinterAutomaticSheetFee d
.OtherPagesTray = wdPrinterAutomaticSheetFee d
End If
End With
Next sec
Application.PrintOut
' Change back to the default printer.
Application.ActivePrinter = strActivePrinter
End Sub
This link details a method for discovering the actual tray numbers:
https://www.experts-exchange.com/questions/21008239/Word-2k-VBA-Macro-to-Select-Paper-Trays-HP-Laserjet-AND-Lexmark-Optra.html
Sub PlainPrint_B()
Dim sec As Section
'
' PlainPrint-B Macro
' Prints to Aficio 2075-B, Plain paper
'
' Get active/default printer.
strActivePrinter = Application.ActivePrinter
'Change the active printer to match local Aficio 2075-B
ActivePrinter = "\\circe\Aficio 2075-B PCL6"
For Each sec In ActiveDocument.Sections
With sec.PageSetup
If sec.Index = 1 Then
.FirstPageTray = wdPrinterUpperBin
.OtherPagesTray = wdPrinterAutomaticSheetFee
Else
.FirstPageTray = wdPrinterAutomaticSheetFee
.OtherPagesTray = wdPrinterAutomaticSheetFee
End If
End With
Next sec
Application.PrintOut
' Change back to the default printer.
Application.ActivePrinter = strActivePrinter
End Sub
This link details a method for discovering the actual tray numbers:
https://www.experts-exchange.com/questions/21008239/Word-2k-VBA-Macro-to-Select-Paper-Trays-HP-Laserjet-AND-Lexmark-Optra.html
ASKER
In my case it is a HP 4350tn
'Tray 2, 1st page Letterhead
.FirstPageTray = 261
'Tray 3, Other pages
.OtherPagesTray = 260
So under your instructions it would look like this??
If sec.Index = 1 Then
.FirstPageTray = 261
.OtherPagesTray = 260
Else
.FirstPageTray = 260
.OtherPagesTray = 260
End If
Great Job, Thank you very much
Hypercat
Yes, but I can't test your exact numbers - I don't have your printer.
All the best, thanks
All the best, thanks
This solution has got me out of a bind aswell.
Can I extend my gratitude to GrahamSkan for his solution, as well as hypercat for asking the question in the first place.
Regards,
Matthew Linnett
Can I extend my gratitude to GrahamSkan for his solution, as well as hypercat for asking the question in the first place.
Regards,
Matthew Linnett
Thanks.
That's why the site is here. Hopefully there have been others in the last six years who found the answer useful.
That's why the site is here. Hopefully there have been others in the last six years who found the answer useful.
4 years on and this issue still rears it's head.
I also thank you Graham for your solution - no other site in my Google search had anything close to this.
I also thank you Graham for your solution - no other site in my Google search had anything close to this.
ASKER
Thanks,
Hypercat