Selecting a printer in a script

I am writing a script that searchs through a Mail Journal and prints to a PDF all messages to/from a particular person. I need to send the output to a specific printer (a PDF driver) and cannot figure out how the print function will do that unattended.

I cannot guarantee that the printer will be defaulted to the proper driver when the script runs so I have to be able to switch to it through the script.

Anyone have any ideas?

Subject: Selecting a printer in a script

I’ve seen this problem before. From what I remember we were not able to decide which printer to use from LotusScript. Some window-API was used to change the default printer of that computer to the PDF Writer, then a print command was run from Notes (using the default printer), then the default printer was returned to it’s previous choise. As far as I can remember the change of the default printer was done chenging som setting in the Windows Registry. Unfortunately I do not remember which windows-API was being used, but I hope this will bring you closer.

Subject: RE: Selecting a printer in a script

Ok, here’s the pieces I use to go to adobe writer and then back to the default printer. I’ll TRY and get all the pieces in here. I got them from some very wonderful person a few years ago, I just can’t remeber who! :slight_smile:

Ok, in the declarations of a script library I have:

Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” ( _

Byval lpAppName As String, _

Byval lpKeyName As String, _

Byval lpDefault As String, _

Byval lpReturnedString As String, _

Byval nSize As Long ) As Long

Declare Function WriteProfileString Lib “kernel32.dll” Alias “WriteProfileStringA” ( _

Byval lpszSection As String, _

Byval lpszKeyName As String, _

Byval lpszString As String ) As Long

Declare Function SendMessage Lib “user32.dll” Alias “SendMessageA” ( _

Byval hwnd As Long, _

Byval wMsg As Long, _

Byval wParam As Long, _

lparam As Long ) As Long

Declare Sub SleepWin Lib “Kernel32.dll” Alias “Sleep” (Byval Milliseconds As Long)

Const ok_info = 64

Const ok_excl = 48

Const yesno_excl = 52

Const ok_stop = 16

Then…in the script to change the printer:

'Set default printer to be Adobe Writer

Dim szPrinter As String

szPrinter = "Acrobat PDFWriter"



On Error Goto ErrorHandler



Const HWND_BROADCAST = &HFFFF

Const WM_WININICHANGE = &H1A

Const WM_SETTINGCHANGE = &H1A



Dim rc As Long

Dim slength As Long

Dim SendCommand As String

Dim oldprinter As String

Dim t As Long

Dim buffer As Variant

buffer = Space(1024)

Dim buflen As Long

buflen = Len(buffer)



rc = GetProfileString( "Devices", szPrinter, "", buffer, buflen )

Dim drivername As String

Dim printerport As String

t = Instr(buffer, ",")

If t > 0 Then 

	drivername = Left(buffer, t - 1 )

	k = Instr(t + 1, buffer, "," )

	If k > 0 Then

		printerport = Mid( buffer, t + 1, k - t - 1 )

	End If

Else

	Messagebox "You do not have the correct printer driver installed for this function.  If the Adobe Acrobat PDFWriter Printer driver has been installed, please rename it to Acrobat PDFWriter."

	Exit Sub

End If

buffer = String(128, 0)

slength = GetProfileString("Windows", "Device", "(error)", buffer, 128)

OldPrinter = Left(buffer, slength)  ' extract the returned string from the buffer

If OldPrinter = "(error)" Then

	Print "Could not read information from WIN.INI."

Else 

	Print "Original Printer: " & OldPrinter

End If

buffer = Space(1024)

buflen = Len(buffer)



rc = GetProfileString( "PrinterPorts", szPrinter, "", buffer, buflen )



i = Instr(buffer, ",")

If i > 0 Then 

	drivername = Left(buffer, i - 1 )

	k = Instr( i + 1, buffer, "," )

	If k > 0 Then

		printerport = Mid( buffer, i + 1, k - i - 1 )

	End If 

End If

Dim newprinter As String

newprinter = szPrinter & "," & drivername & "," & printerport

rc = WriteProfileString( "windows", "Device", NewPrinter)

rc = SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE,0,0)

Call SleepWin(3000)

	'''''''''''''''''''''

I call Intelliprint after that and go through that whole thing, so I don’t know what you’ll have to do. After that’s finished, I change it back to the default printer:

'change default printer back

rc = WriteProfileString( "windows", "Device", OldPrinter)

rc = SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE,0,0)

Call SleepWin(3000)

buffer = String(128, 0)

slength = GetProfileString("Windows", "Device", "(error)", buffer, 128)

NewPrinter = Left(buffer, slength)  ' extract the returned string from the buffer

If NewPrinter = "(error)" Then

	Print "Could not read information from WIN.INI."

Else

	If NewPrinter = OldPrinter Then

		Print "Original Printer: " & OldPrinter

	Else

		While NewPrinter <> OldPrinter

			rc = WriteProfileString( "windows", "Device", OldPrinter)

			rc = SendMessage( HWND_BROADCAST, WM_SETTINGCHANGE,0,0)

			Call SleepWin(3000)

			buffer = String(128, 0)

			slength = GetProfileString("Windows", "Device", "(error)", buffer, 128)

			NewPrinter = Left(buffer, slength)  ' extract the returned string from the buffer

		Wend

	End If

End If

I hope this helps you!

Deb

Subject: RE: Selecting a printer in a script

Hi!

I want to change printer to pdf and print a document in pdf and replace the printer. I try this script but the printer change is doing only when i close notes. When i clic on the botton, the printer is changing to adobe and it’s printing my document on the printer and it’s changing on the old printer. Why my document is printing on printer and not in pdf? I try reloadwindow without success.

Estelle

Subject: Selecting a printer in a script

There’s an WinAPI for setting the default printer:

Const HWND_BROADCAST As Long = &HFFFF&

Const WM_WININICHANGE As Long = &H1A

Declare Function SendNotifyMessage Lib “user32” Alias “SendNotifyMessageA” (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, lParam As Any) As Long

Declare Function SetDefaultPrinter Lib “winspool.drv” Alias “SetDefaultPrinterA” ByVal pszPrinter As String) As Long

'Change the printer by sending the name of the printer as a parameter:

SetDefaultPrinter “Acrobat PDF Writer” 'or what the name of Your printer is…

'Must also notify windows that the change has been made:

Call SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal “windows”)

’ — CODE END —

That’s it!

hth

Subject: RE: Selecting a printer in a script

That is interesting but how would you incorporate it into a lotuscript agent? I am not familiar with the WinAPI.

Subject: Add the script he provided to your agent

Subject: Selecting a printer in a script

If you look in the FAQ of FAQs, printer selection section, you’ll find a complete method to print pdfs. There are a number of other links to various printing functions in the FAQ of FAQs that may be worth your time checking. Finally, a search of this forum and the R4/5 forum will provide even more information on the issues.

HTH

Doug