VBA to set print to default

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
I have an application that prints to PDF then needs to print to the default printer.

Is there any way to identify what the default printer is and then ensure it prints to this printer?

I can't be sure that the activeprinter at the start of the job will be the default so this isn't really an option.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I can't be sure that the activeprinter at the start of the job will be the default so this isn't really an option.

Thanks - I had already explorered that route but when starting the macro I cannot be sure that the last printer was the default.
 
Upvote 0
Here are a few methods to get the default printer name:
Code:
Declare Function GetTheDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" _
                                      (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long
Function GetDefaultPrinter() As String
   ' using API above
   Dim DefPrinter As String, sLen As Long, hResult As Long
   DefPrinter = Space$(255)
   sLen = 255
   hResult = GetTheDefaultPrinter(ByVal DefPrinter, sLen)
   If hResult <> 0 Then GetDefaultPrinter = Left(DefPrinter, sLen - 1)
End Function
Function GetDefaultPrinter2() As String
   ' Using WMI
   Dim scomputer As String
   Dim oWMIService, colPrinters, oItem
   scomputer = "."
   Set oWMIService = GetObject("winmgmts:\\" & scomputer & "\root\cimv2")
   Set colPrinters = oWMIService.ExecQuery("Select * from Win32_Printer", , 48)
   For Each oItem In colPrinters
      If (oItem.Attributes And 4) = 4 Then
         GetDefaultPrinter2 = oItem.Name
         Exit For
      End If
   Next
End Function
Function GetDefaultPrinter3() As String
   ' Using Shell and registry
   Set oShell = CreateObject("WScript.Shell")
   sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
   sDefault = ""
   On Error Resume Next
   sDefault = oShell.RegRead(sRegVal)
   sDefault = Left(sDefault, InStr(sDefault, ",") - 1)
   On Error GoTo 0
   GetDefaultPrinter3 = sDefault
End Function
 
Upvote 0
If you need the port as well (which you probably do here) then you can use this variation of the third function:
Code:
Function GetDefaultPrinter3() As String
   ' Using Shell and registry
   ' output includes port
   Dim oShell, varData
   Dim sRegVal As String, sDefault As String
   Set oShell = CreateObject("WScript.Shell")
   sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
   sDefault = ""
   On Error Resume Next
   sDefault = oShell.RegRead(sRegVal)
   varData = Split(sDefault, ",")
   sDefault = varData(0) & " on " & varData(2)
   On Error GoTo 0
   GetDefaultPrinter3 = sDefault
End Function
 
Upvote 0
thanks rorya,

Just running through and getdefaultprinter3 falls over as oShell is not declared.

What type of variable is it?


Edit - no variavles are defined - Can you confirm them all please
 
Upvote 0
See my last post. (the code was from my VBS samples, hence no declarations)
 
Upvote 0
Code:
Function GetDefaultPrinter3() As String
   ' Using Shell and registry
   ' output includes port
   Dim oShell, varData
   Dim sRegVal As String, sDefault As String
   Set oShell = CreateObject("WScript.Shell")
   sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
   sDefault = ""
   On Error Resume Next
   sDefault = oShell.RegRead(sRegVal)
   varData = Split(sDefault, ",")
   sDefault = varData(0) & " on " & varData(2)
   On Error GoTo 0
   GetDefaultPrinter3 = sDefault
End Function

That is spot on - thanks!!
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,683
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top