Changing Printers in VBA (1 Viewer)

jpl458

Well-known member
Local time
Today, 15:19
Joined
Mar 30, 2012
Messages
1,038
Using ASSESS 2010 and Windows 7.

Have a simple app that normally prints to the default printer. However, I have one button that that scans a plastic card, and I want the result to print to a small receipt printer, then rest the app back to the default (regular printer). I have tried a at least a dozen solutions from the web, none of which work. This seems simple enough, but getting it to work is a nightmare. Here is one example that I have tried, modified, and tried again and again, to no avail. I think I found on this site but not sure since I can't find it again. Here si the complete post.
---------------------------------------------------------------------------------
Yeah, you can write code that enumerates the Printers collection, locates a printer by name (or whatever), and sets this printer as the default printer for Access.
So you can write a function that returns a printer ...
Code:
Function GetPrinter(Name as string) As Access.Printer
dim prt as printer
for each prt in application.printers
if instr(prt.name, Name) then
set GetPrinter = prt
exit function
end if
next
err.raise -1, "GetPrinter()", "Printer not found"
end function
And you can, before you print a report, change the printer ...
Code:
dim prt as printer
set prt = application.printer
set application.printer = GetPrinter("YourNonDefaultPrinterNameHere")
'docmd.openreport "SomeReport"
set application.printer = prt
------------------------------------------------------------------------------
it always fails on this: set application.printer = GetPrinter("YourNonDefaultPrinterNameHere")
Error msg "Expected variable or procedure, not Module"
I put in a proper printer name, that I got from the printer collection.
Also I can never set the application.Printer to anything, examp.
Set Application.Printer ="Fax"
This all very confusing, so any light shed on the problem would really be appreciated.
BTW more than one person has told to just let the user select the printer because this is a real PITA. I don't accept that.

Thanks in advance

jpl
 

sparks80

Physicist
Local time
Today, 23:19
Joined
Mar 31, 2012
Messages
223
Hi,

Allen Browne has written a very neat utility to allow users to select a printer - I know this is not what you want to do, but the code might give you some useful pointers.

http://allenbrowne.com/AppPrintMgt.html

I think this is how you set the printer variable:

Code:
Set prt = Application.Printers(strPrinterName)

And this is how you set the application printer:

Code:
Set Application.Printer = Application.Printers(strPrinterName)
 

spikepl

Eledittingent Beliped
Local time
Tomorrow, 00:19
Joined
Nov 3, 2010
Messages
6,142
And do not call the module in which you put your code the same name as the name of the sub/function. This is what casues your first error. That is in fact what the error message said - it is useful not to panic but actually read the error mesages and try to comprehend them :D
 

Users who are viewing this thread

Top Bottom