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:
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