Print to special printer in background, without changing default printer

Have you ever wanted to print to another printer than the default in Lotus Notes via LotusScript.

If you do that in windows, windows will remember that printer the next time you print in Lotus Notes.

This could be a problem if you have an Application that prints to a specific printer (ex. Labels).
But if you want to print ex. a mail shortly there after, you don’t want it to use that printer.

It all resets if you restart Lotus Notes, but that not an option. Its a windows problem, and not something Lotus Notes can do anything about.

But i have found some code that prints, and sets the printer back to the original again.

Credit goes to Michael R. Barrick.

Here is the code for a button and a class that you can use if you have this problem.

Button:

Use “lib.dmc.print”

Sub Click(Source As Button)
Dim printing As New Printing()
Call printing.PrintDoc()
End Sub

Class:
%REM
Library lib.dmc.print
Created Sep 21, 2011 by Thomas Lindberg – T175260/Laan-Spar
Description: Comments for Library
%END REM
Option Public
Option Declare

Declare Function GetProfileString Lib “kernel32” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

%REM
Class Printing
Description: Comments for Class
%END REM
Class Printing

Public Function PrintDoc()

Dim WshNetwork As Variant
Dim WshPrinters As Variant
Dim pArray() As String
Dim strBuffer As String * 254
Dim iRetValue As Long
Dim DefaultPrinter As String
Dim pCount As Integer
Dim n As Integer
Dim PrinterPath As Variant
Dim ws As New NotesUIWorkspace

‘ Retreive current default printer information
iRetValue = GetProfileString(“windows”, “device”, “,,,”, strBuffer, 254)
DefaultPrinter$ = Left(strBuffer, InStr(strBuffer, “,”) – 1)
Print “Current default printer is ” & DefaultPrinter$

Set WshNetwork = CreateObject(“WScript.Network”)
Set WshPrinters = WshNetwork.EnumPrinterConnections()

‘count the connected printers
pCount% = 0
For n = 1 To WshPrinters.Count() Step 2
If wshPrinters(n) <> “” Then pCount%=pCount%+1
Next

‘collect the names of the connected printers
ReDim pArray(pCount%-1) As String
pCount% = 0
For n = 1 To WshPrinters.Count() Step 2
If wshPrinters(n) <> “” Then
pArray(pCount%) = wshPrinters(n)
pCount%=pCount%+1
End If
Next

PrinterPath = ws.Prompt(4,”Select Printer”,”Choose a printer from the following list:”,””,pArray)
If CStr(PrinterPath)=”” Then Exit function

WshNetwork.SetDefaultPrinter CStr(PrinterPath)

Print “Default printer is now ” & CStr(PrinterPath)

Dim uidoc As NotesUIDocument
Set uidoc = ws.CurrentDocument
Call uidoc.Print( 1, 0, 0, False, PrinterPath )
WshNetwork.SetDefaultPrinter DefaultPrinter$
Print “Default printer restored to ” & DefaultPrinter$

Set WshNetwork = Nothing
Set WshPrinters = Nothing
End function

End Class

This entry was posted in Lotus, Tip and tagged , , , . Bookmark the permalink.