Link to home
Start Free TrialLog in
Avatar of Hypercat (Deb)
Hypercat (Deb)Flag for United States of America

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 = wdPrinterAutomaticSheetFeed".  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 = wdPrinterAutomaticSheetFeed
        'Auto Select tray
        .OtherPagesTray = wdPrinterAutomaticSheetFeed
    End With
    Application.PrintOut
    ' Change back to the default printer.
    Application.ActivePrinter = strActivePrinter
End Sub
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hypercat (Deb)

ASKER

Thank you GrahamSkan you get the points,  The above sugestion worked fine.  Now for the bonus round.  I have another macro for pre-printed letterhead that does the same thing except the .OtherPagesTray point to different tray.  How would you set up the section handling if only the first page of the document goes to tray 1 and all the rest (regardless of sections) goes to tray 2.  My thinking is your sugesstion above would send the first page of each section to tray 1.

Thanks,
Hypercat
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 = wdPrinterAutomaticSheetFeed
            Else
                .FirstPageTray = wdPrinterAutomaticSheetFeed
                .OtherPagesTray = wdPrinterAutomaticSheetFeed
            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


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
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
Thanks.

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.