Link to home
Create AccountLog in
Avatar of David Schmalzer
David SchmalzerFlag for United States of America

asked on

Printing Part 2

How can this code be adjusted to print a selected or multiple selected documents in a view?

Dim result As Variant
      Dim printerName As String
      Dim nPos As String
      
     'Get the printer name
      printerName = String(128, 0)
      result = GetProfileString("WINDOWS", "DEVICE", "", printerName, 127)
      nPos = Instr(printerName, ",")
      printerName = Left(printerName, nPos - 1)
                 'Set the default printer to landscape (printer name, landscape orientation)
      result = SetPrinterOrientation(printerName, 2)
     'Last chance to quit before printing
      Dim answer As Integer
      answer = Msgbox ("Last chance to quit.  Yes to Continue or No to Exit",36,"Continue with printing?")
      If answer <>6 Then Exit Sub
      Dim ws As New NotesUIWorkspace
      Dim uidoc As Notesuidocument
      Set uidoc = ws.currentdocument
      Call uidoc.print(1)
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Selected documents in a view can be found using NotesDatabase.UnprocessedDocuments, from a view action button.

    Dim ns As New NotesSession
    Dim db As NotesDatabase
    Dim dc As NotesDocumentCollection
    Dim doc As NotesDocument

    Set db= ns.CurrentDatabase
    Set dc= db.UnprocessedDocuments
    Set doc= dc.GetFirstDocument
    Do until doc Is Nothing
        ...
        ... ' your code here
        ...
        Set doc= dc.GetNextDocument(doc)
    Loop
Avatar of David Schmalzer

ASKER

Wow, that didn't work. Just to let you know, this button is in a view in a database named document archives. The view is call Yield Import View.  When I select a document and click on the print button after adding your code, it printed a document from my mail inbox! How did that happen?
I forgot to post Global Options and Declarations for the view in case that makes a difference. here it is:

Option Public
Use "PrntLnd"



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

If I close my mailbox, then it tries to print my Welcome page.  Strange.
Brilliant! Where did you put the button? It seems that the current database is NOT the database with the view and the documents. Or db is set elsewhere to some other value...

Can you post the entire code?
Here is the code in it's entirety.  Even though this has been printing in the form  ok, I'm not convinced that it has been calling the Script Library "PrntLnd" because it still doesn't print in landscape. But I'm not worrying about that right now, I just want to be able to print selected docs from a view to a particular printer, or to be able to change the default printer, then change it back when done.

--------------------Script Library Below---------------------------------------------

'PrntLnd:

Option Public

'Script Library

Type DIVT
      quot As Long
      remain As Long
End Type

Private Type PRINTER_DEFAULTS
      pDatatype As Long
      pDevmode As Long
      DesiredAccess As Long
End Type

Private Type PRINTER_INFO_2
      pServerName As Long
      pPrinterName As Long
      pShareName As Long
      pPortName As Long
      pDriverName As Long
      pComment As Long
      pLocation As Long
      pDevmode As Long
      pSepFile As Long
      pPrintProcessor As Long
      pDatatype As Long
      pParameters As Long
      pSecurityDescriptor As Long
      Attributes As Long
      Priority As Long
      DefaultPriority As Long
      StartTime As Long
      UntilTime As Long
      Status As Long
      cJobs As Long
      AveragePPM As Long
End Type

Type DEVMODE
      dmDeviceName As String * 32
      dmSpecVersion As Integer
      dmDriverVersion As Integer
      dmSize As Integer
      dmDriverExtra As Integer
      dmFields As Long
      dmOrientation As Integer
      dmPaperSize As Integer
      dmPaperLength As Integer
      dmPaperWidth As Integer
      dmScale As Integer
      dmCopies As Integer
      dmDefaultSource As Integer
      dmPrintQuality As Integer
      dmColor As Integer
      dmDuplex As Integer
      dmYResolution As Integer
      dmTTOption As Integer
      dmCollate As Integer
      dmFormName As String * 32
      dmUnusedPadding As Integer
      dmBitsPerPel As Integer
      dmPelsWidth As Long
      dmPelsHeight As Long
      dmDisplayFlags As Long
      dmDisplayFrequency As Long
      dmICMMethod As Long
      dmICMIntent As Long
      dmMediaType As Long
      dmDitherType As Long
      dmReserved1 As Long
      dmReserved2 As Long
End Type

Declare Function ClosePrinter Lib "winspool.drv" (Byval hPrinter As Long) As Long

Declare Function DocumentProperties Lib "winspool.drv"_
Alias "DocumentPropertiesA" (Byval hwnd As Long, _
Byval hPrinter As Long, Byval pDeviceName As String, _
pDevModeOutput As Long, pDevModeInput As Long, _
Byval fMode As Long) As Long

Declare Function GetPrinter Lib "winspool.drv" Alias _
"GetPrinterA" (Byval hPrinter As Long, Byval Level As Long, _
pPrinter As Long, Byval cbBuf As Long, pcbNeeded As Long) As Long

Declare Function OpenPrinter Lib "winspool.drv" Alias _
"OpenPrinterA" (Byval pPrinterName As String, phPrinter As Long, _
pDefault As PRINTER_DEFAULTS) As Long

Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (Byval hPrinter As Long,_
Byval Level As Long, pPrinter As Long, Byval commandl As Long) As Long

Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" _
(pDest As DEVMODE, pSource As Long, Byval cbLength As Long)

Declare Sub CopyMemoryBack Lib "Kernel32" Alias "RtlMoveMemory" _
(pDest As Long, pSource As DEVMODE, Byval cbLength As Long)

Declare Sub CopyMemoryPI Lib "Kernel32" Alias "RtlMoveMemory" _
(pDest As PRINTER_INFO_2, pSource As Long, Byval cbLength As Long)

Declare Sub CopyMemoryBackPI Lib "Kernel32" Alias "RtlMoveMemory" _
(pDest As Long, pSource As PRINTER_INFO_2, Byval cbLength As Long)

Declare Function labs Lib "msvcrt.dll" Alias "labs"_
(param As Long) As Long

Declare Function ldiv Lib "msvcrt.dll" (Byval q As Long, divisor As Long) As Long

Function SetPrinterOrientation(Byval sPrinterName As String,Byval orientation As Integer) As Long
      
      Const DM_IN_BUFFER = 8
      Const DM_OUT_BUFFER = 2
      Const PRINTER_ACCESS_ADMINISTER = &H4
      Const PRINTER_ACCESS_USE = &H8
      Const STANDARD_RIGHTS_REQUIRED = &HF0000
      Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
      PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
      
      
      Dim hPrinter As Long
      Dim pd As PRINTER_DEFAULTS
      Dim pinfo As PRINTER_INFO_2
      Dim dm As DEVMODE
      Dim dm2 As DEVMODE
      Dim yDevModeData() As Long
      Dim yPInfoMemory() As Long
      Dim nBytesNeeded As Long
      Dim nRet As Long, nJunk As Long
      Dim q As Long
      
      On Error Goto cleanup
      
 'Open printer with all access to be able to modify settings
      pd.DesiredAccess = PRINTER_ALL_ACCESS
      nRet = OpenPrinter(sPrinterName, hPrinter, pd)
      nRet = DocumentProperties(0, hPrinter, sPrinterName, 0, 0, 0)
      If (nRet < 0) Then
            Msgbox "Cannot get the size of the DEVMODE structure."
            Goto cleanup
      End If
      
      Redim yDevModeData(nRet + 100) As Long
      
      nRet = DocumentProperties(0, hPrinter, sPrinterName, yDevModeData(0), 0, DM_OUT_BUFFER)
      If (nRet < 0) Then
            Msgbox "Cannot get the DEVMODE structure."
            Goto cleanup
      End If
      
      Call CopyMemory(dm, yDevModeData(0), Len(dm))
      dm.dmOrientation = orientation
      Call CopyMemoryBack(yDevModeData(0), dm, Len(dm))
      nRet = DocumentProperties(0, hPrinter, sPrinterName, _
      yDevModeData(0), yDevModeData(0), _
      DM_IN_BUFFER Or DM_OUT_BUFFER)
      
      If (nRet < 0) Then
            Msgbox "Unable to set some settings to this printer."
            Goto cleanup
      End If
      
      Call GetPrinter(hPrinter, 2, 0, 0, nBytesNeeded)
      If (nBytesNeeded = 0) Then Goto cleanup
      
      Redim yPInfoMemory(nBytesNeeded + 100) As Long
      
      nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)
      If (nRet = 0) Then
            Msgbox "Unable to get shared printer settings."
            Goto cleanup
      End If
      
      Call CopyMemoryPI(pinfo, yPInfoMemory(0), Len(pinfo))
      pinfo.pDevmode = labs(yDevModeData(0))
      pinfo.pSecurityDescriptor = 0
      Call CopyMemoryBackPI(yPInfoMemory(0), pinfo, Len(pinfo))
      
      nRet = SetPrinter(hPrinter, 2, yPInfoMemory(0), 0)
      If (nRet = 0) Then
            Msgbox "Unable to set shared printer settings."
      End If
      
      SetPrinterOrientation = nRet
      
cleanup:
      If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
      
End Function

---------------------------View Globals Below--------------------------------------

Option Public
Use "PrntLnd"



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

---------------------------------Click Event / Action Button below -----------------------------

Dim result As Variant
      Dim printerName As String
      Dim nPos As String
      
     'Get the printer name
      printerName = String(128, 0)
      result = GetProfileString("WINDOWS", "DEVICE", "", printerName, 127)
      nPos = Instr(printerName, ",")
      printerName = Left(printerName, nPos - 1)
                 'Set the default printer to landscape (printer name, landscape orientation)
      result = SetPrinterOrientation(printerName, 2)
     'Last chance to quit before printing
      Dim answer As Integer
      answer = Msgbox ("Last chance to quit.  Yes to Continue or No to Exit",36,"Continue with printing?")
      If answer <>6 Then Exit Sub
      Dim ws As New NotesUIWorkspace
      Dim uidoc As Notesuidocument
      Set uidoc = ws.currentdocument
      Call uidoc.print
Desperately looking for the code I supplied... Where is it??

The action button code should be something like:

     Dim result As Variant
     Dim printerName As String
     Dim nPos As String
     Dim ns As New NotesSession
     Dim db As NotesDatabase
     Dim dc As NotesDocumentCollection
     Dim doc As NotesDocument
     Dim ws As New NotesUIWorkspace
     Dim uidoc As Notesuidocument
     
     'Get the printer name
     printerName = String(128, 0)
     result = GetProfileString("WINDOWS", "DEVICE", "", printerName, 127)
     nPos = Instr(printerName, ",")
     printerName = Left(printerName, nPos - 1)
                 'Set the default printer to landscape (printer name, landscape orientation)
     result = SetPrinterOrientation(printerName, 2)
     'Last chance to quit before printing
     Dim answer As Integer
     answer = Msgbox ("Last chance to quit.  Yes to Continue or No to Exit",36,"Continue with printing?")
     If answer <>6 Then Exit Sub

     Set db= ns.CurrentDatabase
     Set dc= db.UnprocessedDocuments
     Set doc= dc.GetFirstDocument
     Do until doc Is Nothing
         Set uidoc= ws.EditDocument(False, doc, True, , True)
         Call uidoc.print
         Call uidoc.Close
         Set doc= dc.GetNextDocument(doc)
     Loop

A warning: this seems to work for a limited number of documents only. Here's a similar question:
    http:Q_21630404.html "Print document from a collection"
Avatar of marilyng
marilyng

Hi sjef_bosman,
You know, we just went through the landscape printing thing in another question.  The calls for landscape do work in XP and W2k, but not for network printers.  And the W2k/XP calls don't work unless you send HWND_BROADCAST, and tell windows that the printer changed, or that the orientation changed.

Another workaround is to configure two printers: one printing in landscape as default, another printing in portrait, and just set the default printer to the one that you want.

The other suggestion that I have with printing uidoc,  uidoc.close(true) <<force close


Regards!
sjef_bosman,
 Here is the previous question:  http:Q_21812713.html#0 "Printing in Landscape" if you need a reference.
I remember the question, my memory serves me well usually, but I rely too much on the full-text search in Notes. The index for a search in EE is days, if not weeks, behind. So thanks for the link!

Anyway, the word "landscape" I never used in the discussion above, since schmad himself said "... it still doesn't print in landscape. But I'm not worrying about that right now ..."
Uh>>printerName = Left(printerName, nPos - 1)
                 'Set the default printer to landscape (printer name, landscape orientation)
     result = SetPrinterOrientation(printerName, 2)<<  what's this?
Ctrl-C, Ctrl-V :$
Yes, the reason I said not worrying about landscape is because I did take the approach of configuring 2 printers, as opposed to setting it in the code itself.  So, I guess the main objective here (for me to print in landscape) is to be able to hit the print button, change the default printer to the configured landscape printer, print the document, then change the default printer back to what it was.  Does that make sense?  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of marilyng
marilyng

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Uh, the correct answer here is sjef's first response.  If you don't mind, please ask CRAK to change the points so that other people referencing the page arent confused by the accepted answer.

Thanks.