Results 1 to 3 of 3
  1. #1
    LonghronJ is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jul 2015

    Change default printer

    I found that to change the default printer, one can use the code below

    Set Application.Printer = Application.Printers(strNewPrinter)
    strNewPrinter = other printer path

    This works well when printing a report within the app. However, when I try to print a pdf file with a given path to my hard drive or out on the network, it always prints to the default printer that is set in my Windows Devices and Printers. I'd presume the above code does not really change the default printer. I know it doesn't because when I run the above code, my default printer does not change. Yes, I have refreshed the list. Anyone have a work-around?

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    put this code in a module , then call
    SetDefaultPrinter "My printer"

    Private Const HWND_BROADCAST As Long = &HFFFF&
    Private Const WM_WININICHANGE As Long = &H1A
    ' The following code allows one to read, and write to the WIN.INI files
    ' In win 2000 the printer settings are actually in the registry. However, windows
    ' handles this correctly
    Private 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
    Private Declare Function WriteProfileString Lib "kernel32" _
       Alias "WriteProfileStringA" _
      (ByVal lpszSection As String, _
       ByVal lpszKeyName As String, _
       ByVal lpszString As String) As Long
    Private Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
      (ByVal hwnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       lparam As Any) As Long
    Function SetDefaultPrinter(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)
         SetDefaultPrinter = 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")
         SetDefaultPrinter = False
      End If
    End Function

  3. #3
    LonghronJ is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Ranman, I think I'm missing fstrDField()

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Print to other than default printer with code
    By dmgg in forum Programming
    Replies: 13
    Last Post: 08-28-2015, 06:14 PM
  2. Select Default Printer
    By pkstormy in forum Code Repository
    Replies: 4
    Last Post: 03-10-2015, 09:26 AM
  3. Replies: 3
    Last Post: 02-25-2014, 11:46 AM
  4. Print to Default Printer
    By bsc0617 in forum Access
    Replies: 7
    Last Post: 10-10-2013, 07:46 AM
  5. Trying to Change Default printer
    By sims.anderson2010 in forum Programming
    Replies: 2
    Last Post: 03-08-2012, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Other Forums: Microsoft Office Forums - Senior Forums