[VBA] GetPrinterPort no longer working in W10 / Excel 2016?

NessPJ

Active Member
Joined
May 10, 2011
Messages
431
Office Version
  1. 365
Hello all,

After an upgrade to Office 2016 and Windows 10 on several computers i noticed that the code i use to determine the printerport in my VBA routine no longer works.
Does anyone know how to fix this?

Code:
Public Function GetPrinterPort(strPrinterName As String) As String
   Dim objReg As Object, strRegVal As String, strValue As String
   Const HKEY_CURRENT_USER = &H80000001
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
   strRegVal = "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
   objReg.getstringvalue HKEY_CURRENT_USER, strRegVal, strPrinterName, strValue
   GetPrinterPort = Split(strValue, ",")(1)
End Function

I tried changing the regkey location to "Software\Microsoft\Windows NT\CurrentVersion\Devices" because i read about that somewhere, but i'm guessing that just holds the device lists rather than the printer ports.
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
The code definitely works on that set up. Could it be that the the names of the printers have changed? Have you checked what’s actually under that key using regedit?
 
Upvote 0
The code definitely works on that set up. Could it be that the the names of the printers have changed? Have you checked what’s actually under that key using regedit?

Hi, you're right! Just did some more troubleshooting with the user. They added the wrong printer (PRT011 on Server Y instead of Server X).
Confirmed that this still works on W10 + Office 2016. Problem solved!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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