Automated Printer Selection & Page Setup

SkyCraw

Registered User.
Local time
Yesterday, 20:05
Joined
Oct 9, 2013
Messages
100
Good Friday afternoon everyone,

Is it possible, within an OnOpen even of a specific report, to set the printer selection and page setup (size, margins, etc.) to certain things? If so, how would I go about accomplishing this?

I would like to do this for specific users within our domain (this I won't have any trouble with).

Any help would be greatly appreciated! :)
 
It is possible to do this in the OnOpen event, checkout "application.Printer.DeviceName" and the other members of Printer.
I think it would be easier to select a custom printer for your report, the drawback is that if the printer is not available you get a warning.
 
The easiest way (only way?) is to set the system 's default printer to the required printer temporarily, then set it back to the original when the report has finished.

the report details you mention would generally be preset at design time, but you can change them at run time.
 
Thanks Gemma and Peter, both of your answers will help get me on the right track. I would use a custom printer, but I have end users in different locations of our facilities that use this database and report. It's even trickier to set for global use when the report requires tabloid paper...

This is a mimic of what I was thinking about doing:

Code:
Private Sub Report_Open(Cancel As Integer)

Dim netuser As String

netuser = UCase(NetworkUserName)

If netuser = "A" or netuser = "B" or netuser = "C" Then
    Dim prtDefault As Printer
    Set Application.Printer = Application.Printers(0)
    'have code here to ensure tabloid paper size and landscape orientation
ElseIf netuser = "D" or netuser = "E" or netuser = "F" Then
    Dim prtDefault As Printer
    Set Application.Printer = Application.Printers(1)
    'have code here to ensure tabloid paper size and landscape orientation
ElseIf netuser = "G" or netuser = "H" or netuser = "I" Then
    Dim prtDefault As Printer
    Set Application.Printer = Application.Printers(2)
    'have code here to ensure tabloid paper size and landscape orientation
End If

End Sub
 
I already have a function to fetch the NetworkUserName as well, in case you were wondering.
 
here are the two subs I use. The setprinter function looks for a printer device with the same name as the printer you want to use, then sets the active printer to be that printer. If it can't find the printer, it just leaves the default printer as active.

I have an info message in there, which can be taken out simply enough.

after printing, clearprinter returns the printer to the default.

Hope this helps


Code:
Function setprinter(requiredprinter As String, preview As Boolean) As Boolean

'requiredprinter is the name of the printer you want to use
'preview is a setting to determine whether to preview or not
'this sub selects the printer and returns control to the calling app, ready for the print to start

 Dim ptrname As String
Dim ptr As Printer
Dim useprinter As Printer

'try and find this printer
For Each ptr In Application.Printers
     If ptr.DeviceName = requiredprinter Then
         Set useprinter = ptr
         GoTo gotit
      End If
Next
 
noprinter:
       MsgBox ("The expected printer: " & requiredprinter & " was not found. The " & rep & " will be set to print to your default printer. ")
        Exit Function

gotit:
        On Error GoTo badPrinter
        Set Application.Printer = useprinter

'i added this bit as a generic bit of information about the print.
'this can be commented out if not required

         If preview Then
                 MsgBox ("The printer selected for this report has been set to printer " & ptrname & ". " & vbCrLf & vbCrLf & _
"The print preview will now open. You can select a different printer if you require. ")
          Else
                  MsgBox ("The printer selected for this report has been set to printer " & ptrname & ". " & vbCrLf & vbCrLf & _
"The print will now start automatically on this printer. ")
           End If
           Exit Function

badPrinter:
            MsgBox ("There was an error setting the printer to " & ptrname & vbCrLf & _
"The report will print on your default printer. " & vbCrLf & vbCrLf & _
"Error: " & Err & " Desc: " & Err.Description)

End Function

  
 Function clearprinter() As Boolean
'reassign the printer to the default
         Set Application.Printer = Nothing
End Function
 
Thanks for all the help, fellow husky! :)

It works awesome, just need to take it one step further... could I make it set the paper size to tabloid after selecting the appropriate printer as default?
 
After setting the printer use

Code:
set application.Printer.PaperSize = 3 [COLOR="SeaGreen"]'constant is acPRPSTabloid[/COLOR]
 
Thanks for all the help, Peter and gemma :).

One last issue I'm having is when I initially run this report, even though it sets the printer properly and I'm specifying tabloid paper, it still displays the report in letter because it's still gathering paper sizes from my Windows default printer. However, when I run it a second time, it selects the proper printer and paper size.

I've tried several workarounds (programically reopening the report, setting the printer twice, etc.) but to no avail; would any of you know a way around this?
 
Never mind, just had to move my code calling gemma's functions from my reports OnOpen event to the button itself on my Main Menu :3

Thanks again for all the help! :D
 

Users who are viewing this thread

Back
Top Bottom