Results 1 to 11 of 11

Thread: [RESOLVED] ShellExecute and print properties

  1. #1

    Thread Starter
    Fanatic Member paulorton's Avatar
    Join Date
    Aug 2006
    Location
    West Wales
    Posts
    809

    Resolved [RESOLVED] ShellExecute and print properties

    Is there a way to specify Print properties in code when attempting to print a Word doc? I'm using ShellExecute, which doesn't appear to give any control over the actual printing (I want to be able to print on both sides of the paper, flipping on long edge) but maybe there's another way?
    Paul Orton
    VB6
    Visual Web Developer 2008 Express Edition
    Microsoft Visual Basic 2012 Express

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: ShellExecute and print properties

    how about using Common Dialog and ShowPrint?

    Or since this a word doc, you may not have to use ShellExecute.
    IIF(Post.Rate > 0 , , )

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: ShellExecute and print properties

    you would need to set the properties for the default printer before calling shellexecute, then set them back after printing
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Fanatic Member paulorton's Avatar
    Join Date
    Aug 2006
    Location
    West Wales
    Posts
    809

    Re: ShellExecute and print properties

    Quote Originally Posted by zeezee
    how about using Common Dialog and ShowPrint?
    I'll look into this but what I really want is for the printing to happen each time without a dialog requiring attention.

    Quote Originally Posted by zeezee
    Or since this a word doc, you may not have to use ShellExecute.
    What would I use instead?
    Paul Orton
    VB6
    Visual Web Developer 2008 Express Edition
    Microsoft Visual Basic 2012 Express

  5. #5

    Thread Starter
    Fanatic Member paulorton's Avatar
    Join Date
    Aug 2006
    Location
    West Wales
    Posts
    809

    Re: ShellExecute and print properties

    Quote Originally Posted by westconn1
    you would need to set the properties for the default printer before calling shellexecute, then set them back after printing
    I see there is a Duplex property of the Printer object - would that be all I need to set? What is its syntax - Printer.Duplex = True, perhaps?
    Paul Orton
    VB6
    Visual Web Developer 2008 Express Edition
    Microsoft Visual Basic 2012 Express

  6. #6
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: ShellExecute and print properties

    You can manipulate Wordoc, without opening word and print it.
    Code:
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
            wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
            ManualDuplexPrint:=False, Collate:=True, Background:=True, PrintToFile:= _
            False, PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
            PrintZoomPaperHeight:=0
    This I got from recording a macro. I ll try to get some full vb code, but also look into FAQ in the forum and there are articles on using Word.

    IIF(Post.Rate > 0 , , )

  7. #7
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: ShellExecute and print properties

    OK I did something for you. Try this app. Mind that I have Office 2003,
    Change the references according to your version of office.

    I cant test for duplex as i dont have a printer. So try changing to ManualDuplexPrint:=True

    Some articels if you are interested.

    http://www.vbforums.com/showthread.php?t=402093
    http://www.vbforums.com/showthread.php?t=358585
    http://vbnet.mvps.org/index.html?cod...ultprinter.htm

    Attached Files Attached Files
    IIF(Post.Rate > 0 , , )

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: ShellExecute and print properties

    I see there is a Duplex property of the Printer object - would that be all I need to set? What is its syntax - Printer.Duplex = True, perhaps?
    no, that would only change the property of the vb6 apps printer property, not the default windows printer
    vb Code:
    1. printer.duplex = 1  ' 0 for off or 2 for other direction
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Fanatic Member paulorton's Avatar
    Join Date
    Aug 2006
    Location
    West Wales
    Posts
    809

    Re: ShellExecute and print properties

    Thanks, everyone, for the help. With ideas from you (plus some MS code I have adapted) the following seems to provide what I wanted (ie. print Word doc invisibly, with or without duplex). Maybe someone else will find it useful, too.


    Code:
    Public Type PRINTER_DEFAULTS
    
           pDatatype As Long
           pDevmode As Long
           DesiredAccess As Long
       End Type
    
       Public 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       ' Pointer to DEVMODE
           pSepFile As Long
           pPrintProcessor As Long
           pDatatype As Long
           pParameters As Long
           pSecurityDescriptor As Long  ' Pointer to SECURITY_DESCRIPTOR
           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
    
       Public 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
    
       Public Const DM_DUPLEX = &H1000&
       Public Const DM_IN_BUFFER = 8
    
       Public Const DM_OUT_BUFFER = 2
       Public Const PRINTER_ACCESS_ADMINISTER = &H4
       Public Const PRINTER_ACCESS_USE = &H8
       Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
       Public Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
                 PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
    
       Public Declare Function ClosePrinter Lib "winspool.drv" _
        (ByVal hPrinter As Long) As Long
       Public Declare Function DocumentProperties Lib "winspool.drv" _
         Alias "DocumentPropertiesA" (ByVal hwnd As Long, _
         ByVal hPrinter As Long, ByVal pDeviceName As String, _
         ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, _
         ByVal fMode As Long) As Long
       Public Declare Function GetPrinter Lib "winspool.drv" Alias _
         "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
         pPrinter As Byte, ByVal cbBuf As Long, pcbNeeded As Long) As Long
       Public Declare Function OpenPrinter Lib "winspool.drv" Alias _
         "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, _
         pDefault As PRINTER_DEFAULTS) As Long
       Public Declare Function SetPrinter Lib "winspool.drv" Alias _
         "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, _
         pPrinter As Byte, ByVal Command As Long) As Long
       Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (pDest As Any, pSource As Any, ByVal cbLength As Long)
    	
    	
    	
        Private Sub Command1_Click()
    	  PrintAssociatedFile FileToPrint, DuplexParam  'set this to 1 (none),2 (duplex long) or 3 (duplex short)
        End Sub
    	
    	
    	
    	Public Sub PrintAssociatedFile(Filename As String, DuplexParam As Integer)
    
    		Dim oWord As Object
    		Dim oDoc As Object
    
    		Set oWord = CreateObject("Word.application")
    
    		oWord.Visible = False
    
    		Set oDoc = oWord.Documents.Open(Filename)
    		oDoc.Range.Select
    
    		SetPrinterDuplex Printer.DeviceName, DuplexParam
    
    		oDoc.PrintOut Background:=False
    
    		SetPrinterDuplex Printer.DeviceName, 1
    		
    		oDoc.Saved = True
    		oDoc.Close
    		Set oDoc = Nothing
    
    		oWord.Quit
    		Set oWord = Nothing
    
    	End Sub
    	
    	
    	
    	Public Function SetPrinterDuplex(ByVal sPrinterName As String, ByVal nDuplexSetting As Long) As Boolean
    ' ==================================================================
       ' SetPrinterDuplex
       '
       '  Programmatically set the Duplex flag for the specified printer
       '  driver's default properties.
       '
       '  Returns: True on success, False on error. (An error will also
    
       '  display a message box. This is done for informational value
       '  only. You should modify the code to support better error
       '  handling in your production application.)
       '
       '  Parameters:
       '    sPrinterName - The name of the printer to be used.
       '
       '    nDuplexSetting - One of the following standard settings:
       '       1 = None
       '       2 = Duplex on long edge (book)
       '       3 = Duplex on short edge (legal)
       '
       ' ==================================================================
          Dim hPrinter As Long
          Dim pd As PRINTER_DEFAULTS
          Dim pinfo As PRINTER_INFO_2
          Dim dm As DEVMODE
       
          Dim yDevModeData() As Byte
          Dim yPInfoMemory() As Byte
          Dim nBytesNeeded As Long
          Dim nRet As Long, nJunk As Long
       
          On Error GoTo cleanup
       
          If (nDuplexSetting < 1) Or (nDuplexSetting > 3) Then
             MsgBox "Error: dwDuplexSetting is incorrect."
             Exit Function
          End If
          
          pd.DesiredAccess = PRINTER_ALL_ACCESS
          nRet = OpenPrinter(sPrinterName, hPrinter, pd)
          If (nRet = 0) Or (hPrinter = 0) Then
             If Err.LastDllError = 5 Then
                MsgBox "Access denied -- See the article for more info."
             Else
                MsgBox "Cannot open the printer specified " & _
                  "(make sure the printer name is correct)."
             End If
             Exit Function
          End If
       
          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 Byte
          nRet = DocumentProperties(0, hPrinter, sPrinterName, _
                      VarPtr(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))
       
          If Not CBool(dm.dmFields And DM_DUPLEX) Then
            MsgBox "You cannot modify the duplex flag for this printer " & _
                   "because it does not support duplex or the driver " & _
                   "does not support setting it from the Windows API."
             GoTo cleanup
          End If
       
          dm.dmDuplex = nDuplexSetting
          Call CopyMemory(yDevModeData(0), dm, Len(dm))
       
          nRet = DocumentProperties(0, hPrinter, sPrinterName, _
            VarPtr(yDevModeData(0)), VarPtr(yDevModeData(0)), _
            DM_IN_BUFFER Or DM_OUT_BUFFER)
    
          If (nRet < 0) Then
            MsgBox "Unable to set duplex setting 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 Byte
    
          nRet = GetPrinter(hPrinter, 2, yPInfoMemory(0), nBytesNeeded, nJunk)
          If (nRet = 0) Then
             MsgBox "Unable to get shared printer settings."
             GoTo cleanup
          End If
       
          Call CopyMemory(pinfo, yPInfoMemory(0), Len(pinfo))
          pinfo.pDevmode = VarPtr(yDevModeData(0))
          pinfo.pSecurityDescriptor = 0
          Call CopyMemory(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
       
          SetPrinterDuplex = CBool(nRet)
    
    cleanup:
          If (hPrinter <> 0) Then Call ClosePrinter(hPrinter)
       
    End Function
    Paul Orton
    VB6
    Visual Web Developer 2008 Express Edition
    Microsoft Visual Basic 2012 Express

  10. #10
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: [RESOLVED] ShellExecute and print properties

    Oh this is cool. You finally found it.
    Cheers!!!
    IIF(Post.Rate > 0 , , )

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] ShellExecute and print properties

    interesting, when i run this code the second call to documentproperties shuts down my vb ide, i will have to look at it more later
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width