Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing to multiple printers 2

Status
Not open for further replies.

ordy2

Programmer
Sep 21, 2002
6
GB
I want to print a report to all but one of the workstations printers

The code I have used is :

For Each Y In Printers
If UCase(Y.DeviceName) <> UCase(&quot;LaserJet 2200 DN&quot;) Then
set printer = Y
Set CrxReport = crxApplication.OpenReport (App.Path &quot;\Preport.Rpt&quot;)
CrxReport.DiscardSavedData
CrxReport.ParameterFields.GetItemByName(&quot;Pdate&quot;).AddCurrentValue Yesterday
CrxReport.PrintOut False, 1
End If
Next

the problem this only prints to the default printer each time.

I have tried just printing text to the printers in the following code and it works fine. Why??? does the crystal alter the behaviour?

For Each x In Printers
Debug.Print x.DeviceName
Set Printer = x
Printer.Print &quot;this is text&quot;
Printer.EndDoc

Next
 
I think the problem may have been caused by the set printer statement is for your application only and crystal report runs on its own env. You may try to permanently set the printer then call up crystal report by adding a common dialog box on your form, call it cdlPrinter. Then insert the following codes before setting printer:

Set oDefPrinter = Printer
cdlPrinter.PrinterDefault=True

After you finish printing, rest the default printer:

Set Printer = oDefPrinter
cdlPrinter.PrinterDefault = False

Let me know if this works

Jason



 
Hi Jason

Thanks for your suggestion, I have have tried [see code below], but unless I add the line &quot;cdlPrinter.ShowPrinter&quot;
it still outputs to the default printer.


Set OriginalPrinter = Printer
For Each Y In Printers
If UCase(Y.DeviceName) <> UCase(&quot;LaserJet 2200 DN&quot;) Then
Set oDefPrinter = Y
cdlPrinter.ShowPrinter
cdlPrinter.PrinterDefault = True
Set CrxReport = crxApplication.OpenReport(App.Path & &quot;\Preport.Rpt&quot;)
CrxReport.DiscardSavedData
CrxReport.ParameterFields.GetItemByName(&quot;Pdate&quot;).AddCurrentValue Yesterday
Debug.Print Y.DeviceName
' CrxReport.PrintOut False, 1
Printer.EndDoc
End If
Next
Set Printer = OriginalPrinter
cdlPrinter.PrinterDefault = False
 

Try out this API

[tt]Private Declare Function SetDefaultPrinter Lib &quot;winspool.drv&quot; Alias &quot;SetDefaultPrinterA&quot; (ByVal PrinterName As String) As Boolean
[/tt]

You pass in the name of the printer you want to become the default printer for the system.

[tt]Printer.DeviceName[/tt]

Good Luck
 
u can trace all printer in the system and then use a for to print at one at a time
 
vb5prgmr

You solution was perfect. I don't know how much time I have spent on this but I'm glad that, with your help, I now have a solution.

Many thanks for your help

Ian
 
Sorry to raise this subject again. But having finished the coding and rolling it out to the target machines I find the code fails with the following error &quot; run-time error 453&quot; Can't find dll entry point SetDefaultPrinterA in winspool drv.&quot; A search on Microsofts TechNet infers a syntax error but I can't see any problem with the code line.

The program works fine on my machine win2K but fails on the NT machines it's being installed to.

Can anyone help?

Thanks
 

from help...

SetDefaultPrinter
The SetDefaultPrinter function sets the printer name of the default printer for the current user on the local computer.

BOOL SetDefaultPrinter(
LPCTSTR pszPrinter // default printer name
);
Parameters
pszPrinter
[in] Pointer to a null-terminated string containing the default printer name. For a remote printer, the name format is \\server\printername. For a local printer, the name format is printername.
If this parameter is NULL or an empty string, that is, &quot;&quot;, SetDefaultPrinter does nothing if there is already a default printer. However, if there is no default printer, SetDefaultPrinter sets the default printer to the first printer, if any, in an enumeration of printers installed on the local computer.

Return Values
If the function succeeds, the return value is a nonzero value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
To set the default printer on Windows 2000, call SetDefaultPrinter. To set the default printer to earlier operating systems, call GetProfileString, WriteProfileString, and SendNotifyMessage, as shown in the following code:

// read PrinterPorts section from win.ini
// returned string should be of the form &quot;driver,port,timeout,timeout&quot;, i.e. &quot;winspool,LPT1:,15,45&quot;.
GetProfileString(TEXT(&quot;Windows&quot;),TEXT(&quot;Device&quot;),_T(&quot;,,,&quot;),m_szName, COUNTOF(m_szName));
WriteProfileString(TEXT(&quot;Windows&quot;),TEXT(&quot;Device&quot;),pszPrinterName ));
// Notify all open applications of the change. Note, only applications that handle the message will recognize the change.
#if WM_WININICHANGE != WM_SETTINGCHANGE
// Old message type; for windows95
SendNotifyMessage(HWND_BROADCAST,WM_WININICHANGE,0,(LPARAM)szWindows);
#endif
// New message type
SendNotifyMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,(LPARAM)szWindows);

When using this method, you must specify a valid printer, driver, and port. If they are invalid, the APIs do not fail but the result is not defined. This could cause other programs to set the printer back to the previous valid printer. You can use EnumPrinters to retrieve the printer name, driver name, and port name of all available printers.

Requirements
Windows NT/2000 or later: Requires Windows 2000 or later.
Windows 95/98/Me: Unsupported.

Header: Declared in Winspool.h; include Windows.h.
Library: Use Winspool.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows 2000.

See Also
Printing and Print Spooler Overview, Printing and Print Spooler Functions, EnumPrinters, GetDefaultPrinter, GetProfileString, WriteProfileString, SendNotifyMessage



So it looks like you are going to have to check for what OS you program is on and then use the correct method. You can search this site for code to check which OS your program is running on.

Good Luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top