Help improve my variable network printer code?

NessPJ

Active Member
Joined
May 10, 2011
Messages
420
Office Version
  1. 365
Hey guys,

After some searching and experimenting i have found some code here, that would run in my Excel Sheet and find a network printer (independant of client desktops).

I have the code here:
Code:
    'The current default printer is defined and stored.    
    Dim DefaultPrinter As String
    DefaultPrinter = Application.ActivePrinter

    'This routine allows you to address a Network printer independant of the Desktop Client:
    Dim q As Long, xUNC As String
    On Error Resume Next
    For q = 0 To 99
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne" & Format(q, "00") & ":"
    Application.ActivePrinter = xUNC
    If Err.Number = 0 Then 'if UNC is valid, print
    
    'The Page Setup for the printing assignment is defined here. Some settings are not available to default printers:
     With Sheets("VVAAH2").PageSetup
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .LeftMargin = Application.InchesToPoints(0.708661417322835)
    .RightMargin = Application.InchesToPoints(0.708661417322835)
    .TopMargin = Application.InchesToPoints(0.15748031496063)
    .BottomMargin = Application.InchesToPoints(0.748031496062992)
    .HeaderMargin = Application.InchesToPoints(0.31496062992126)
    .FooterMargin = Application.InchesToPoints(0.31496062992126)
    .PaperSize = 342
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    End With
    
    'Print de Pagina af met de correcte Netwerkprinter.
    Sheets("VVAAH2").PrintOut Copies:=1, ActivePrinter:= _
    xUNC
        
    Else
        Err.Clear
    End If
        Next q
        On Error GoTo 0

    'The stored default printer is selected again.
    Application.ActivePrinter = DefaultPrinter

The Server + Printername are referred to a Cell in my worksheet, so they can be parameter controlled (call it ready-for-the-future functionality).

Now i have noticed there are still some desktops which will not work because the Windows Network name still seems to deviate.
In this case i noticed the extension of the name was no longer " on Ne0"#:" but " on e0#:".

I was wondering if there was a way to make the VBA code above loop through both types of network names? Or i could rule out the "N" to be optional in the printer name somehow?

Thanks in advance! :)
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Basically the code posted above will do the following right now:

Rich (BB code):
Loop through:
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne01":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne02:
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne03:
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne04:
Etc.

Until it hits the right printserver and the printing works (Err.Number becomes 1 and it exits).

What i would like for the code to do is the following:
Rich (BB code):
Loop through:
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne01":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on e01":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne02":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on e02":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on Ne03":
    xUNC = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on e03":
Etc.

Until it hits the right printserver and the printing works (Err.Number becomes 1 and it exits).

Any help on how to do this please? :)
It seems like its the only thing left that prevents my printer code from working everwhere....
 
Upvote 0
You don't need to rely on Errors to get the correct printer port. I wrote a post on it here, but Rory's solution is probably more versatile:

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

Which can be used:
Code:
MsgBox GetPrinterPort("\\Server\Printer Name")
 
Upvote 0
Dear Kyle,

Thanks for your reply!

strPrinterName will be the entire printername independent of the client machine the user is on?

Before i try, any experience running this on virtual desktop environments (VDI) ? :)
 
Upvote 0
No, it will return the port number for a printer. So the "NE01:" "NE02:" etc.

Nope, give it a whirl ;)
 
Upvote 0
Seems to be working on a virtual machine as well. Cool!
 
Upvote 0
I have some more detailed information about my problem, i hope you can help me.
Right now my code looks like this (Module1):

Rich (BB 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

Rich (BB code):
Dim Port, VVAPrinter As String
    Port = GetPrinterPort(Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value)
    VVAPrinter = Sheets("Admin").Range("B26").Value & Sheets("Admin").Range("B14").Value & " on " & Port
    Application.ActivePrinter = VVAPrinter

    'De Pagina instellingen van de af te drukken vrachtbrief worden elke maal gestuurd.
    With Sheets("VVAPA1").PageSetup
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .LeftMargin = Application.InchesToPoints(0.708661417322835)
    .RightMargin = Application.InchesToPoints(0.708661417322835)
    .TopMargin = Application.InchesToPoints(0.15748031496063)
    .BottomMargin = Application.InchesToPoints(0.748031496062992)
    .HeaderMargin = Application.InchesToPoints(0.31496062992126)
    .FooterMargin = Application.InchesToPoints(0.31496062992126)
    .PaperSize = 342
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    End With
    
    'Print de pagina met de correcte Netwerkprinter.
    Sheets("VVAPA1").PrintOut Copies:=1, ActivePrinter:=VVAPrinter

I keep getting a debug error on the orange line. Any help would be greatly appreciated!
 
Upvote 0

Forum statistics

Threads
1,216,575
Messages
6,131,501
Members
449,654
Latest member
andz

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