VBA to Print Labels but Issues Selecting Printer

4starpur

New Member
Joined
Oct 23, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

Let me first say I have a rudimentary knowledge of VBA at best, but I'm really hoping someone can help. We have an old script that was written for warehouse employees to print labels for orders. The issue is that the script will randomly change to other printers so labels arent going to the label makers and are printing on the regular printers. Sometimes just changing the default printer in windows settings will fix it for a few days. We have tried changing the active printers in the code but it will still send it to the wrong printer. I changed the printer name below just in case.

Again, I didnt write this so I'm sure there are better ways to do this but this works for what we need. I'm just looking for any obvious issues that may be causing it to occasionally change/fail.

Thanks for any help!


Here is the VBA:

Private Sub CommandButton1_Click()

Dim total As Integer
Dim pallets As Integer
Dim boxes As Integer
Dim rolls As Integer
Dim cnt As Integer
Dim labnum As Interior

Dim sCurrentPrinter As String
Dim sourcebook As Workbook

Set sourcebook = ThisWorkbook

If Not IsNumeric(Sheets("Data").Range("B2").Value) Then
MsgBox "You must enter in a number in the Boxes field."
Exit Sub
End If

If Not IsNumeric(Sheets("Data").Range("B3").Value) Then
MsgBox "You must enter in a number in the Rolls field."
Exit Sub
End If

If Not IsNumeric(Sheets("Data").Range("B4").Value) Then
MsgBox "You must enter in a number in the Rolls field."
Exit Sub
End If

boxes = Sheets("Data").Range("B2").Value
rolls = Sheets("Data").Range("B3").Value
pallets = Sheets("Data").Range("B4").Value
total = boxes + rolls + pallets

If total = 0 Then
MsgBox "You did not enter a number for boxes, rolls, or pallets, so no label will print."
Exit Sub
End If

'Turn off screen updating and save active printer
Application.ScreenUpdating = False
sCurrentPrinter = Application.ActivePrinter

'Clear as we are about to print boxes amount
Sheets("Label").Range("C6").ClearContents
Sheets("Label").Range("C7").ClearContents
Sheets("Label").Range("C8").ClearContents

Sheets("Label").Range("C9").Value = total

'PRINT BOXES AMOUNTS
cnt = boxes
labelnum = 1

Do While cnt > 0
Sheets("Label").Range("C6").Value = CStr(labelnum) + " of " + CStr(boxes)
Sheets("Label").PrintOut Copies:=1, ActivePrinter:="PRINTER XYZ"
cnt = cnt - 1
labelnum = labelnum + 1
Loop

Sheets("Label").Range("C6").ClearContents
Sheets("Label").Range("C7").ClearContents
Sheets("Label").Range("C8").ClearContents

'PRINT ROLLS AMOUNTS
cnt = rolls
labelnum = 1

Do While cnt > 0
Sheets("Label").Range("C7").Value = CStr(labelnum) + " of " + CStr(rolls)
Sheets("Label").PrintOut Copies:=1, ActivePrinter:="PRINTER XYZ"
cnt = cnt - 1
labelnum = labelnum + 1
Loop

Sheets("Label").Range("C6").ClearContents
Sheets("Label").Range("C7").ClearContents
Sheets("Label").Range("C8").ClearContents

'PRINT PALLETS AMOUNTS
cnt = pallets
labelnum = 1

Do While cnt > 0
Sheets("Label").Range("C8").Value = CStr(labelnum) + " of " + CStr(pallets)
Sheets("Label").PrintOut Copies:=1, ActivePrinter:="PRINTER XYZ"
cnt = cnt - 1
labelnum = labelnum + 1
Loop

Sheets("Label").Range("C6").ClearContents
Sheets("Label").Range("C7").ClearContents
Sheets("Label").Range("C8").ClearContents
Sheets("Label").Range("C9").ClearContents

Sheets("Data").Range("B1").ClearContents
Sheets("Data").Range("B2").ClearContents
Sheets("Data").Range("B3").ClearContents
Sheets("Data").Range("B4").ClearContents

Sheets("Data").Range("B1").Select

'Turn on screen updating and set printer back
Application.ActivePrinter = sCurrentPrinter
Application.ScreenUpdating = True

'This way it doesn't ask to save it
sourcebook.Saved = True

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
VBA Code:
    Application.Dialogs(xlDialogPrinterSetup).Show
    sCurrentPrinter = Application.ActivePrinter
...


then change

Sheets("Label").PrintOut Copies:=1, ActivePrinter:="PRINTER XYZ"
to
Sheets("Label").PrintOut Copies:=1, ActivePrinter:=sCurrentPrinter
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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