Hi,
when i try to run the following code i get a runtime error 1004 "Unable to set active printer property of the application class" at the line "ExcelObject.ActivePrinter = "\\WS149.Logitech.local\DYMO LabelManager PC" ".
The code is meant to open an excel sheet, add a template and put some numbers in, print to the label printer(which is not the default printer) and then close again. if i leave this problem line out it works fine but prints to the default printer. Any ideas anyone?
i posted this question the other day and got no replies, this time ill try explain my code a bit more.
Private Sub PrintGoodsLabels_Click()
Dim NoOfLabels As Long
Dim I As Integer
Dim ExcelObject As Object
Dim XLSheet As Object
Dim boolAddAddress As Boolean
Dim Response As String
Dim YesNo As Integer
Const lpClassName = "XLMain"
YesNo = vbYes
HowMany:
'set number of labels to print
Response = InputBox("How Many Labels would you like to print for" & _
Chr(13) & Chr(10) & "Job Number: " & Me.JobNo & _
Chr(13) & Chr(10) & "Inwards Goods Number: " & Me.IGNo, "Print Labels")
'checking for valid input
If (Not Response = "") And (IsNumeric(Response)) Then
NoOfLabels = Eval(Response)
NoOfLabels = Round(NoOfLabels)
'checking for valid input
If (NoOfLabels < 0) Then
MsgBox "Please enter a positive number", vbOKOnly, "Error"
GoTo HowMany
End If
'checking for valid input
If NoOfLabels > 256 Then
YesNo = MsgBox("You can only Print 256 labels at a time." & _
Chr(13) & Chr(10) & "Would you like to Print 256 Labels?", vbYesNo, "Error")
If YesNo = vbYes Then
NoOfLabels = 256
End If
ElseIf NoOfLabels > 50 Then
YesNo = MsgBox("Are you sure you want to print " & NoOfLabels & " labels?", vbYesNo)
If YesNo = vbYes Then
NoOfLabels = 256
End If
End If
If (FindWindow(lpClassName, 0&) = 0) And (YesNo = vbYes) Then 'check if excel is not open
Set ExcelObject = CreateObject("excel.application")
ExcelObject.Visible = True
ExcelObject.Workbooks.Add (LOGIGOODS_LABEL_TEMPLATE)
Set XLSheet = ExcelObject.Application.ActiveWorkbook.ActiveSheet
'inputting cells for specified number of labels
For I = 1 To NoOfLabels
With XLSheet
.Cells(1, I).Value = "IG:" & Me.IGNo 'from form
.Cells(2, I).Value = "Job:" & Me.JobNo 'from form
End With
Next I
'PROBLEM LINE: should set the printer for excel to print to the label printer, have tried different formatts of name with no luck.
ExcelObject.ActivePrinter = "\\WS149.Logitech.local\DYMO LabelManager PC"
'print and close the excel sheet and excel itself
XLSheet.PrintOut ExcelObject.Application.ActiveWorkbook.Close SaveChanges:=False
ExcelObject.Application.Quit
ElseIf YesNo = vbNo Then
GoTo HowMany
Else 'excel is open, dont want to stuff with any open sheets
MsgBox "Please Close Microsoft Excel And Try Again", vbOKOnly, "Error"
End If
'if nothing is entered or cancel button pressed then do nothing
ElseIf (Response = "") Then
Else
'if invalid input ask for somethiing valid
MsgBox "Please enter a number.", vbOKOnly, "Error"
GoTo HowMany
End If
End Sub
Thanks again
Anthony
when i try to run the following code i get a runtime error 1004 "Unable to set active printer property of the application class" at the line "ExcelObject.ActivePrinter = "\\WS149.Logitech.local\DYMO LabelManager PC" ".
The code is meant to open an excel sheet, add a template and put some numbers in, print to the label printer(which is not the default printer) and then close again. if i leave this problem line out it works fine but prints to the default printer. Any ideas anyone?
i posted this question the other day and got no replies, this time ill try explain my code a bit more.
Private Sub PrintGoodsLabels_Click()
Dim NoOfLabels As Long
Dim I As Integer
Dim ExcelObject As Object
Dim XLSheet As Object
Dim boolAddAddress As Boolean
Dim Response As String
Dim YesNo As Integer
Const lpClassName = "XLMain"
YesNo = vbYes
HowMany:
'set number of labels to print
Response = InputBox("How Many Labels would you like to print for" & _
Chr(13) & Chr(10) & "Job Number: " & Me.JobNo & _
Chr(13) & Chr(10) & "Inwards Goods Number: " & Me.IGNo, "Print Labels")
'checking for valid input
If (Not Response = "") And (IsNumeric(Response)) Then
NoOfLabels = Eval(Response)
NoOfLabels = Round(NoOfLabels)
'checking for valid input
If (NoOfLabels < 0) Then
MsgBox "Please enter a positive number", vbOKOnly, "Error"
GoTo HowMany
End If
'checking for valid input
If NoOfLabels > 256 Then
YesNo = MsgBox("You can only Print 256 labels at a time." & _
Chr(13) & Chr(10) & "Would you like to Print 256 Labels?", vbYesNo, "Error")
If YesNo = vbYes Then
NoOfLabels = 256
End If
ElseIf NoOfLabels > 50 Then
YesNo = MsgBox("Are you sure you want to print " & NoOfLabels & " labels?", vbYesNo)
If YesNo = vbYes Then
NoOfLabels = 256
End If
End If
If (FindWindow(lpClassName, 0&) = 0) And (YesNo = vbYes) Then 'check if excel is not open
Set ExcelObject = CreateObject("excel.application")
ExcelObject.Visible = True
ExcelObject.Workbooks.Add (LOGIGOODS_LABEL_TEMPLATE)
Set XLSheet = ExcelObject.Application.ActiveWorkbook.ActiveSheet
'inputting cells for specified number of labels
For I = 1 To NoOfLabels
With XLSheet
.Cells(1, I).Value = "IG:" & Me.IGNo 'from form
.Cells(2, I).Value = "Job:" & Me.JobNo 'from form
End With
Next I
'PROBLEM LINE: should set the printer for excel to print to the label printer, have tried different formatts of name with no luck.
ExcelObject.ActivePrinter = "\\WS149.Logitech.local\DYMO LabelManager PC"
'print and close the excel sheet and excel itself
XLSheet.PrintOut ExcelObject.Application.ActiveWorkbook.Close SaveChanges:=False
ExcelObject.Application.Quit
ElseIf YesNo = vbNo Then
GoTo HowMany
Else 'excel is open, dont want to stuff with any open sheets
MsgBox "Please Close Microsoft Excel And Try Again", vbOKOnly, "Error"
End If
'if nothing is entered or cancel button pressed then do nothing
ElseIf (Response = "") Then
Else
'if invalid input ask for somethiing valid
MsgBox "Please enter a number.", vbOKOnly, "Error"
GoTo HowMany
End If
End Sub
Thanks again
Anthony