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
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
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.
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
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