Dynamically change network printer port

MrTinkertrain

Board Regular
Joined
Feb 7, 2007
Messages
66
Office Version
  1. 365
  2. 2021
Hello gurus,

I have the following problem.
I have this piece of code to print some sheets on a network printer :

Code:
Application.ScreenUpdating = False
Sheets("nadrukorder print").Visible = True
Sheets(Array("archiefmap", "nadrukorder print")).Select

Application.ActivePrinter = "HP LaserJet 3390 Series PCL 6 op Ne03:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

Sheets("nadrukorder").Activate
Sheets("nadrukorder print").Visible = False
Application.ScreenUpdating = True

This code does the trick, but for some unknown reason that printer port (NE03: ) changes every now and then (??)
Yesterday I needed to set it to NE02: and today I had to set it to NE03: to make this code work.

In order to prevent this problem I would like to change that printer port dynamically.
I have been searching for a solution and I found a function which shows the network printing port :

Code:
 Public Function GetPrinterPort2(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
    GetPrinterPort2 = Mid$(strValue, 10, 5)
End Function

Can someone help me out with the combination of those two snippets of code ?
I've been trying to solve it for a few hours, but I get stuck ...

Thanx in advance

Best regards,
Mike
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hmm, that looks familiar... :)

You would need something like:
Code:
Dim strPrinter as String
strPrinter = "HP LaserJet 3390 Series PCL 6"
Application.ScreenUpdating = False
Sheets("nadrukorder print").Visible = True
Sheets(Array("archiefmap", "nadrukorder print")).Select

Application.ActivePrinter = strPrinter & " op " & GetPrinterPort2(strPrinter)
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

Sheets("nadrukorder").Activate
Sheets("nadrukorder print").Visible = False
Application.ScreenUpdating = True
 
Upvote 0
Wow, you're quick :)

I've searched so many sites for this one, so I didn't remember where I got the function from

So i'm glad you recognize it ;)

Works like a jiffy, rorya

Thanx for your quick solution :D
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,138
Members
452,381
Latest member
Nova88

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