Results 1 to 5 of 5

Thread: [RESOLVED] About Printer...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    21

    Resolved [RESOLVED] About Printer...

    hello folks, I have a problem,
    I want to make a sub that changes the form and number of copies of the printer for the next print job, like:

    Private Sub SetPrinterForm(formName As String, copies As Long)

    I did the following code with no effects, I don't find where is the error,
    please, if you know where is it, please tell me,
    thanks a lot, Angel.

    Code:
    Private Sub SetPrinterForm(formName As String, copies As Long)
        Dim hPrinter As Long
        Dim cbRequired As Long
        Dim pd As PRINTER_DEFAULTS
        Dim pi2 As PRINTER_INFO_2
        Dim buff() As Long
        Dim dm As DEVMODE
        pd.DesiredAccess = PRINTER_ACCESS_USE Or PRINTER_ACCESS_ADMINISTER
        If (OpenPrinter(VB.Printer.DeviceName, hPrinter, pd) <> 0) Then
            If (GetPrinter(hPrinter, 2, 0&, 0&, cbRequired) = 0) Then
                ReDim buff(cbRequired) As Long
                If GetPrinter(hPrinter, 2, buff(0), cbRequired, cbRequired) <> 0 Then
                    Call CopyMemory(pi2, buff(0), Len(pi2))
                    dm = pi2.pDevMode
                    dm.dmFields = 0
                    dm.dmFields = DM_FORMNAME Or DM_COPIES
                    dm.dmFormName = formName & Chr(0)
                    dm.dmCopies = copies
                    pi2.pDevMode = dm
                    Call SetPrinter(hPrinter, 2, pi2, 0&)
                End If
            End If
            Call ClosePrinter(hPrinter)
        End If
    End Sub
    Last edited by angelherriv; Oct 30th, 2007 at 06:23 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: About Printer...

    What error are you getting?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    21

    Re: About Printer...

    with the above code, I have no error, the problem is that it do nothing, and I need to change printer form and copies at the same time
    I would like for example, get a code to change the form and number of copies of a printing job using Win32 APIs, without make use of VB.Printer.Copies

    using SetPrinter method,

    thanks in advance, Angel.
    Last edited by angelherriv; Oct 30th, 2007 at 06:31 PM.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    21

    Wink Re: About Printer...

    Hello folks I have solved the problem,
    I did some modifications in the code; in few words, this code allow you to set the form and number of copies to the current printer,
    I hope this be useful for somebody sometime, see you soon, Angel.

    put this in a VB Module:
    Code:
    Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
    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, pDevModeOutput As Any, pDevModeInput As Any, ByVal fMode As Long) As Long
    Public Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal cbBuf As Long, pcbNeeded As Long) As Long
    Public Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long
    Public Const PRINTER_ACCESS_ADMINISTER = &H4
    Public Const PRINTER_ACCESS_USE = &H8
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const CCHDEVICENAME = 32
    Public Const CCHFORMNAME = 32
    Public Const DM_IN_BUFFER = 8
    Public Const DM_OUT_BUFFER = 2
    Public Const DM_FORMNAME = &H10000
    Public Const DM_COPIES = &H100&
    Public Const DM_MODIFY = 8
    Public Const DM_COPY = 2
    Public Type DEVMODE
        dmDeviceName As String * CCHDEVICENAME
        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 * CCHFORMNAME
        dmUnusedPadding As Integer
        dmBitsPerPel As Long
        dmPelsWidth As Long
        dmPelsHeight As Long
        dmDisplayFlags As Long
        dmDisplayFrequency As Long
    End Type
    Public Type ACL
        AclRevision As Byte
        Sbz1 As Byte
        AclSize As Integer
        AceCount As Integer
        Sbz2 As Integer
    End Type
    Public Type SECURITY_DESCRIPTOR
        Revision As Byte
        Sbz1 As Byte
        Control As Long
        Owner As Long
        Group As Long
        Sacl As ACL
        Dacl As ACL
    End Type
    Public Type PRINTER_INFO_2
        pServerName As String
        pPrinterName As String
        pShareName As String
        pPortName As String
        pDriverName As String
        pComment As String
        pLocation As String
        pDevMode As Long
        pSepFile As String
        pPrintProcessor As String
        pDatatype As String
        pParameters As String
        pSecurityDescriptor As 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 PRINTER_DEFAULTS
        pDatatype As String
        pDevMode As DEVMODE
        DesiredAccess As Long
    End Type
    
    Public Sub SetPrinterForm(formName As String, Optional copies As Long = 1)
        Dim hPrinter As Long
        Dim cbRequired  As Long
        Dim pd As PRINTER_DEFAULTS
        Dim pi2 As PRINTER_INFO_2
        Dim buff() As Byte
        Dim dm As DEVMODE
        Dim bDevMode() As Byte
        pd.DesiredAccess = STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE
        If (OpenPrinter(VB.Printer.DeviceName, hPrinter, pd) <> 0) Then
            If (GetPrinter(hPrinter, 2&, 0&, 0&, cbRequired) = 0) Then
                ReDim buff(1 To cbRequired) As Byte
                If (GetPrinter(hPrinter, 2, buff(1), cbRequired, cbRequired) <> 0) Then
                    Call CopyMemory(pi2, buff(1), Len(pi2))
                    ReDim bDevMode(1 To cbRequired)
                    If (pi2.pDevMode) Then
                        Call CopyMemory(bDevMode(1), ByVal pi2.pDevMode, Len(dm))
                    Else
                        Call DocumentProperties(0&, hPrinter, sPrnName, bDevMode(1), 0&, DM_OUT_BUFFER)
                    End If
                    Call CopyMemory(dm, bDevMode(1), Len(dm))
                    dm.dmFormName = formName & Chr(0)
                    dm.dmCopies = copies
                    dm.dmFields = DM_FORMNAME Or DM_COPIES
                    Call CopyMemory(bDevMode(1), dm, Len(dm))
                    Call DocumentProperties(0&, hPrinter, sPrnName, bDevMode(1), bDevMode(1), DM_IN_BUFFER Or DM_OUT_BUFFER)
                    pi2.pDevMode = VarPtr(bDevMode(1))
                    Call SetPrinter(hPrinter, 2, pi2, 0&)
                End If
            End If
            Call ClosePrinter(hPrinter)
        End If
    End Sub
    
    Last edited by angelherriv; Oct 31st, 2007 at 11:31 AM.

  5. #5
    New Member
    Join Date
    Oct 2009
    Posts
    5

    Re: [RESOLVED] About Printer...

    Angel,

    Good afternoon.

    Can this code be tweaked so that it can assist MS Access in detecting whether an Access report has been sent to the printer?

    My objective is to trigger some code when my report has been sent to the printer and not to the screen.

    My code is in the "PageHeaderSection" of the report under the OnPrint event.

    It works fine but I only need it to execute one time and that is when the user actually sends the report to the printer (i.e. File -> Print). I know that the Print dialog can be open using the Docmd object but it does not expose any of the Print dialog's methods or properties.

    Is there any way to adjust this code to assist MS Access in detecting whether the Print dialog is open?

    Thanks,

    BigJohnT

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