Looping through Ne:xx options to find correct port

learningthings

New Member
Joined
Oct 29, 2021
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hi everyone, this is the code I have so far:
VBA Code:
Application.ScreenUpdating = False
Application.ActivePrinter = "RecieptPrinter on Ne00:"
Dim strCurrentPrinter As String
strCurrentPrinter = Application.ActivePrinter
   With Sheets("Invoice")
      .Visible = True
      .PrintOut Copies:=1, Collate:=True
      .Visible = False
   End With
Application.ScreenUpdating = True

The problem is that occasionally the port for the printer changes. I need to specify the ports because I have two printers connected to the same computer.
Is there a way to code the system to loop through Ne00 - Ne99 until it finds a match?
Any other methods or suggestions are appreciated!!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about something like this:

VBA Code:
Sub AutoSetPrinterPortFromPrinterName()
'
    Dim PrinterName         As String
    Dim strCurrentPrinter   As String
    Dim strsetting          As String
    Dim PrinterPort         As Variant
'
    Application.ScreenUpdating = False
'
    PrinterName = "RecieptPrinter"                                          ' <--- Set this to the name of your printer that you want to use
'
    strsetting = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices\" & PrinterName)
    PrinterPort = Split(strsetting, ",")
'
    Application.ActivePrinter = PrinterName & " on " & PrinterPort(1)
'
    strCurrentPrinter = Application.ActivePrinter
'
    With Sheets("Invoice")
        .Visible = True
        .PrintOut Copies:=1, Collate:=True
        .Visible = False
    End With
'
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@learningthings Does the code I submitted not satisfy your desire to automatically set the proper port for the printer name that you selected?
 
Upvote 0
Please explain the couldn't get to work. Did the code result in a run time error?
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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