print macro to increase port number

mrnacar

Board Regular
Joined
Jan 27, 2010
Messages
188
Office Version
  1. 365
Platform
  1. Windows
The following macro works fine except the port number is limited only to 9. So if I have 10 or more print devices, then the macro won't work if the device we're looking for is on port 10 or above. Is there a way to write it so that it will work up to maybe 15 devices?


Code:
Sub printform()
Dim WS As Worksheet
Set WS = ActiveSheet
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
For i = 0 To 9
Err.Clear
On Error Resume Next
Application.ActivePrinter = "LGLDOUBLE on Ne0" & i & ":"
If Err.Number = 0 Then GoTo printnow
On Error GoTo 0
Next
On Error GoTo 0
Exit Sub
printnow:
    Dim iCopies As String
    
    iCopies = Application.InputBox("How many copies do you want to print?", Type:=2)
    
    If iCopies <> vbNullString Then
    
        WS.PrintOut Copies:=iCopies
    Else
        Exit Sub
    End If
ActivePrinter = sCurrentPrinter
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
AFAIK, your code could be changed from...

For i = 0 To 9

to...

For i = 0 To 12

or more if you have more print devices
 
Upvote 0
I don't think that will work because it will return as port 012 instead of 12.

AFAIK, your code could be changed from...

For i = 0 To 9

to...

For i = 0 To 12

or more if you have more print devices
 
Upvote 0
That's okay, I figured it out myself. But if someone can shorten the code then I can learn something. Thank you.

Code:
Sub printform()
Dim WS As Worksheet
Set WS = ActiveSheet
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
For i = 0 To 9
Err.Clear
On Error Resume Next
Application.ActivePrinter = "LGLDOUBLE on Ne0" & i & ":"
If Err.Number = 0 Then GoTo printnow
On Error GoTo 0
Next
If i = 10 Then Call printform2
On Error GoTo 0
Exit Sub
printnow:
    Dim iCopies As String
    
    iCopies = Application.InputBox("How many copies do you want to print?", Type:=2)
    
    If iCopies <> vbNullString Then
    
        WS.PrintOut Copies:=iCopies
    Else
        Exit Sub
    End If
ActivePrinter = sCurrentPrinter
End Sub
Sub printform2()
Dim WS As Worksheet
Set WS = ActiveSheet
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
For i = 10 To 20
Err.Clear
On Error Resume Next
Application.ActivePrinter = "LGLDOUBLE on Ne" & i & ":"
If Err.Number = 0 Then GoTo printnow
On Error GoTo 0
Next
If i = 21 Then MsgBox "Contact 'DID' to setup your printer"
If i = 21 Then Exit Sub
On Error GoTo 0
Exit Sub
printnow:
    Dim iCopies As String
    
    iCopies = Application.InputBox("How many copies do you want to print?", Type:=2)
    
    If iCopies <> vbNullString Then
    
        WS.PrintOut Copies:=iCopies
    Else
        Exit Sub
    End If
ActivePrinter = sCurrentPrinter
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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