Change default printer programmatically (Access 2003)

T

TomT

I am wondering if this can be done

We have a network printer installed which creates pdf files out of whatever is printed. Right now, users preview a report, and if they want to print to this pdf printer, they have to go File\Print then choose this printer from the list

I was hoping to put a button on a toolbar, which would 1) make sure they had this printer installed, 2) change the application default printer to it, 3) print the report, and 4) if necessary reset the default printer back to what it was

I have the code to see if the printer is there, and, as I understand it, set the default printer to the pdf printer. However running the code crashes Access, so I guess I'm doing something wrong, or perhaps this cannot be done

Here's the basic code to find out if the printer is there, and if it is, set the default printer

Public Function SetPDFPrinter() As Boolea

Dim prtDefaultName As Strin
Dim prtDefault As Printe
Dim prtLoop As Printe
Dim foundPDF As Boolea

'get the current default printe
Set prtDefaultOrig = Application.Printe

For Each prtLoop In Application.Printer

With prtLoo

If .DeviceName Like "*pdfFactory Pro" The

foundPDF = Tru
SetPDFPrinter = Tru
prtDefaultName = .DeviceNam
Set prtDefault = Application.Printers(prtDefaultName
Exit Fo

End I

End Wit

Next prtLoo

If foundPDF = True The

Set Application.Printer = prtDefaul

End I

End Functio

This is called from a simple sub, which is fired by a button on the toolbar (visible only when the report is being previewed

Public Sub PrintRpt(

If SetPDFPrinter = True The

DoCmd.RunCommand acCmdPrin

End I

End Su

Having been working with Access 97 for the last 7 years or so, I am not all that familiar with working with the new printer objects

If there's a better way to do this, I'd be grateful to hear it

Thanks

To
 
N

Nelson

Go to VB help index and type prtdevmode. The Microsoft KB will give you
even more detailed info on how to use prtdevmode.

TomT said:
I am wondering if this can be done:

We have a network printer installed which creates pdf files out of
whatever is printed. Right now, users preview a report, and if they want to
print to this pdf printer, they have to go File\Print then choose this
printer from the list.
I was hoping to put a button on a toolbar, which would 1) make sure they
had this printer installed, 2) change the application default printer to it,
3) print the report, and 4) if necessary reset the default printer back to
what it was.
I have the code to see if the printer is there, and, as I understand it,
set the default printer to the pdf printer. However running the code crashes
Access, so I guess I'm doing something wrong, or perhaps this cannot be
done.
Here's the basic code to find out if the printer is there, and if it is, set the default printer:

Public Function SetPDFPrinter() As Boolean

Dim prtDefaultName As String
Dim prtDefault As Printer
Dim prtLoop As Printer
Dim foundPDF As Boolean

'get the current default printer
Set prtDefaultOrig = Application.Printer

For Each prtLoop In Application.Printers

With prtLoop

If .DeviceName Like "*pdfFactory Pro" Then

foundPDF = True
SetPDFPrinter = True
prtDefaultName = .DeviceName
Set prtDefault = Application.Printers(prtDefaultName)
Exit For

End If

End With

Next prtLoop

If foundPDF = True Then

Set Application.Printer = prtDefault

End If

End Function

This is called from a simple sub, which is fired by a button on the
toolbar (visible only when the report is being previewed)
Public Sub PrintRpt()

If SetPDFPrinter = True Then

DoCmd.RunCommand acCmdPrint

End If

End Sub

Having been working with Access 97 for the last 7 years or so, I am not
all that familiar with working with the new printer objects.
 
T

TomT

Nelson,

Thanks for your reply. I was aware of the method you kindly suggested,
however I was under the impression that this could be accomplished
with the printers collection/printer object in Access 2003. Perhaps I
am misunderstanding the purpose and use of these features.

It appears from the documentation that the application default printer
can be changed using the printer object. Am I missing something?

Thanks for your help,

TomT
 
B

Billy Yao [MSFT]

Hi Tom,

From your desciption, I havn't yet known what the error you recieved with that code, but I saw that you didn't
set the printer object Nothing before/after looping through the Application.Printers collections.

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set Application.Printer = Nothing
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I'm not 100% sure that this will cause the issue. However, we really can change the application default
printer with the printer object. See the following KB article:

284286 How to Reset Changes to the Application.Printer Object
http://support.microsoft.com/?id=284286

Another possible cause of this problem is the wildcard character ' * ' in the IF condition. Access 97 allowed *
but this is not the ansi standard which expects % in Access 2000 or later. When the ExtendedAnsiSQL flag is
turned on, Jet 4.0 uses the SQL-92 reserved words list. In this way, we need to use SQL wildcard character
' % ' and ' _ ' instead of ' * ' and ' ? '.

Using the ExtendedAnsiSQL Connection String Attribute (Access Driver):
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/odbc/htm/odbcjetaccessusing_the_extendedansisql_connection_string_attribute.asp

For more information on how to choose a default printer for a report, please refer to the following KB articles,
with which we can change the default printer, set teh different pater and inherite the printer properties:

208840 ACC2000: How to Use Code to Change a Report's Printer
http://support.microsoft.com/?id=208840

200546 ACC2000: How to Print a Report to Different Paper Trays
http://support.microsoft.com/?id=200546

290293 Printer Properties Not Inherited by Objects That Use the Default Printer
http://support.microsoft.com/?id=290293

Does that answer your question Tom? Please apply my suggestions above and let me know if this helps
resolve your problem. If there is anything more I can do to assist you, please feel free to post it in the group.

Regards,

Billy Yao
Microsoft Online Support
 
T

TomT

Billy,

Thanks very much for your reply. What I would like to do is place a button on a toolbar, which runs code to change the default printer to a specific printer, then print the report.

The report would already be open in print preview. After changing the default printer, I'm trying to find the command which would actually print (without showing the select printer, print setup screen, like file\print), in the same way as pushing the printer button.

The print button just prints the report directly (from print preview), but I can't seem to find a VB function to accomplish this. Docmd.RunCmd.AcCmdPrint opens the printer screen. If I can find the correct command, I will be able to test this properly.

Thanks for your assistance in this,

Tom
 
T

TomT

Billy

I just tested setting the default printer, and used the Docmd.Printout method. I got this error

Title: Microsoft Visual C++ Runtime Librar
Runtime Error
Program: ...files\Microsoft Office\Office 2003\Office11\MSACCESS.EX
abnormal program terminatio

Click OK on this error causes Access to shutdown

Tom
 
S

SA

Tom:

If you've already set the target printer before the report is previewed,
then all you need is:

DoCmd.SelectObject, acReport, "YourReportNameHere" '(or
Screen.ActiveReport.Name)
Docmd.PrintOut

Thats all there is to it.

The problem is trying to change the default or target printer once the
report is opened in preview. When a report is first opened, it "reads" the
printer information for Access' default (if its set to use the default) or
its own stored printer information (if its set to use a specific printer).
It NEVER reads this information again. So if you change the printer while
the report is open it WILL NOT pick this information up and it will be sent
to the printer that was originally the set when it opened. There are only
two work arounds for this:

1.) Use Docmd.RunCommand acCmdPrint 'Which will bring up the print dialog

2.) Set the printer from your tool bar and then cause the previewed report
to close (either literally, or by going to design mode), send the report to
print, and then reopen in preview.

No other options will work.
 
S

SA

Tom:

Unfortunately some printer drivers are better 'network' printers than
others, exposing the proper device information. Have you tried this with
pdfFactory installed locally rather than as a network connection?
 
T

TomT

Steve

Unfortunately even if that worked, it would not be an option. The printer is set up as a network printer, and quite a few people use it. Installing the driver on each indivdual system is not practical at this time

Tom
 
B

Billy Yao [MSFT]

Tom,

I agree with Steve that there are some known issues which we would fail to get the information from the printer
due to the a network environment. Could you have a try to install the driver locally on ONE of the individual
systems. In this way, we may isolate the issue and see if it is indeed network related.

We appreciate your cooperation and look forward to hearing from you.

Best regards,

Billy Yao
Microsoft Online Support
 

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