Results 1 to 5 of 5

Thread: How to change default printer in VBA

  1. #1
    Join Date
    Jan 2009
    Posts
    133

    How to change default printer in VBA

    Hi friends,

    I have heard from my friends that we can change the default printer in Excel by using VBA.I had try to find over the internet for a solution but was not able to get it.So had made a post over here.


    Intel Core 2 Duo 2.8 Ghz
    G31 Asrock Motherboard with Intel chipset
    2gb RAM
    256 Nvidia Graphic Card
    200gb HDD

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to change default printer in VBA

    Sure i will help you out with this.It's very simple we need to do it by changing the property Application.Active Printer.

    Code:
    Sub PrintToAnotherPrinter()
    Dim strCurrentPrinter As String
        strCurrentPrinter = Application.ActivePrinter ' store the current active printer
        On Error Resume Next ' ignore printing errors
        Application.ActivePrinter = "microsoft fax on fax:" ' change to another printer
        ActiveSheet.PrintOut ' print the active sheet
        Application.ActivePrinter = strCurrentPrinter ' change back to the original printer
        On Error GoTo 0 ' resume normal error handling
    End Sub

  3. #3
    Join Date
    Jan 2009
    Posts
    133

    Re: How to change default printer in VBA

    Thanks for replying me i have try use the code but it's not working for me for more information i am trying to make My Network printer as Default printer.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to change default printer in VBA

    To print to a network Printer you need to first get the full network printer name and after that you can get a print for the worksheet.


    Code:
    Sub PrintToNetworkPrinterExample()
    Dim strCurrentPrinter As String, strNetworkPrinter As String
        strNetworkPrinter = GetFullNetworkPrinterName("HP LaserJet 8100 Series PCL")
        If Len(strNetworkPrinter) > 0 Then ' found the network printer
            strCurrentPrinter = Application.ActivePrinter
    
           'Change to the network printer
    
            Application.ActivePrinter = strNetworkPrinter
            Worksheets(1).PrintOut  print something
    
        ' Change back to the previously active printer
    
            Application.ActivePrinter = strCurrentPrinter
        End If
    End Sub
    
    
    
    Function GetFullNetworkPrinterName(strNetworkPrinterName As String) As String
    
    Dim strCurrentPrinterName As String, strTempPrinterName As String, i As Long
        strCurrentPrinterName = Application.ActivePrinter
        i = 0
        Do While i < 100
            strTempPrinterName = strNetworkPrinterName & " on Ne" & Format(i, "00") & ":"
            On Error Resume Next  'Try to change to the network printer
            Application.ActivePrinter = strTempPrinterName
            On Error GoTo 0
            If Application.ActivePrinter = strTempPrinterName Then
    
               'The network printer was found
    
                GetFullNetworkPrinterName = strTempPrinterName
            
                i = 100  makes the loop end
            End If
            i = i + 1
        Loop
    
        'Remove the line below if you want the function to change the active printer
    
    End Function

  5. #5
    Join Date
    Aug 2010
    Location
    SYdney, Australia
    Posts
    1

    Re: How to change default printer in VBA

    What 'References' are required. I have code:
    "Application.ActivePrinter = P_Reqd_Printer"
    but get a compile error "Method or data member not found" with the word 'ActivePrinter' highlighted.

    In VBA under Tools/References, I have references to "Find Printers type library", "Microsoft Office 11.0 Object Library" and "Windows Script Host Object Model" (among others).
    What are the pre-requisites?
    David

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2010, 05:26 PM
  2. how to change default printer on domain computer
    By Rihanna in forum Hardware Peripherals
    Replies: 3
    Last Post: 22-11-2010, 10:20 PM
  3. Vista - default printer doesn't stay as default
    By Freddie Kang in forum Vista Help
    Replies: 8
    Last Post: 19-04-2010, 03:12 AM
  4. Change Work field default to change hours, not duration?
    By Dharitree in forum Microsoft Project
    Replies: 6
    Last Post: 03-11-2009, 03:58 AM
  5. Cannot Set Default Printer
    By Flynnn007 in forum Vista Help
    Replies: 2
    Last Post: 05-03-2008, 06:45 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,642,417,495.32061 seconds with 17 queries