Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2014
    Posts
    1

    Unanswered: Need help setting printer in VBA for server app

    I have an application in Microsoft Access 2010 that is going to run on a server. I automatically import an Excel spreadsheet to the application and then print the new records out to a printer. But, I need to print the new records out to a printer close by the people who will be processing these reports that are produced. I use a macro to import the spreadsheet and one to print the reports. But I need to specify a specific printer. Not sure how to do this. Help?

  2. #2
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    495
    Provided Answers: 24
    The printer is controled by the user. If you are running the reports, then you will need a user list with the printer they are near.

    Change printer by: SetMyDefaultPrinter rst.[printerName]
    Code:
    
    Public Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
    
    Function SetMyDefaultPrinter(strPrinterName As String) As Boolean
    
       Dim strDeviceLine As String
       Dim strBuffer     As String
       Dim lngbuf        As Long
        
      ' get the full device string
      '
       strBuffer = Space(1024)
       lngbuf = GetProfileString("PrinterPorts", strPrinterName, "", strBuffer, Len(strBuffer))
      
      'Write out this new printer information in
      ' WIN.INI file for DEVICE item
      If lngbuf > 0 Then
         
         strDeviceLine = strPrinterName & "," & _
                         fstrDField(strBuffer, Chr(0), 1) & "," & _
                         fstrDField(strBuffer, Chr(0), 2)
                         
         Call WriteProfileString("windows", "Device", strDeviceLine)
         SetMyDefaultPrinter = True
         
         ' Below is optional, and should be done. It updates the existing windows
         ' so the "default" printer icon changes. If you don't do the below..then
         ' you will often see more than one printer as the default! The reason *not*
         ' to do the SendMessage is that many open applications will now sense the change
         ' in printer. I vote to leave it in..but your case you might not want this.
         '
         
         'Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, ByVal "windows")
        
      Else
         SetMyDefaultPrinter = False
      End If
    End Function

  3. #3
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Select a specific printer while building the report, you do not need a single line of VBA for that. If you actually need to change the printer of a report dynamically, you can use (e.g. in the Report_Open event):
    Code:
    Me.Printer.DeviceName = "<Printer Name>"
    Where <Printer Name> is the name of the printer.
    Have a nice day!

Tags for this Thread

Posting Permissions

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