提问人:St3althPatchin 提问时间:11/15/2023 更新时间:11/15/2023 访问量:66
VBA 打印脚本中的对象变量错误
Object Variable Error in VBA Printing Script
问:
我最近将一个 Excel vba 项目从我的个人计算机移动到我的工作计算机。此项目旨在使用 Brother 打印机进行连接和打印。我不断收到运行时错误“91”:未设置对象变量或具有块变量。这阻碍了我使用工作计算机打印任何内容。但是当我在个人计算机上连接并使用 vba 时,它似乎运行良好,并且不会发生错误。虽然在单步执行代码时,我在“局部变量”窗口中没有看到任何指示该问题的内容。
Dim CreateLabel As VbMsgBoxResult
Dim labelInfo As String
Dim sPath As String
Dim ObjDoc As Object
' Step 1: Ask the user if they wish to create a label
CreateLabel = MsgBox("Do you wish to create a label?", vbYesNo)
If CreateLabel = vbYes Then
' Step 2: Prompt for each parameter
Dim licensePlate As String
Dim partNumber As String
Dim Description As String
Dim expirationDate As String
Dim validInput As Boolean
Dim userInput As String
' Step 2a: Enter SupplierPlateID with exit option
'Do
'userInput = InputBox("Enter SupplierPlateID" & vbNewLine & _
"Press 'Cancel' to terminate the process.")
'If userInput = "" Then Exit Sub
'licensePlate = userInput
licensePlate = InputBox("Enter License Plate:")
'validInput = ValidateSupplierPlateID(licensePlate)
'If Not validInput Then
' MsgBox "Invalid SupplierPlateID."
'End If
'Loop While Not validInput
' Step 2b: Enter Part Number with exit option
Do
userInput = InputBox("Enter Part Number (must have two '-' and end with '-875')" & vbNewLine & _
"Press 'Cancel' to terminate the process.")
If userInput = "" Then Exit Sub
partNumber = userInput
validInput = ValidatePartNumber(partNumber)
If Not validInput Then
MsgBox "Invalid Part Number. It must have two '-' and end with '-875'."
End If
Loop While Not validInput
Description = InputBox("Enter Description:")
' Step 2c: Enter Expiration Date with exit option
expirationDate = InputBox("Enter Experation Date (YYYY-MM-DD)" & vbNewLine & _
"Press 'Cancel' to terminate the process.")
' Step 3: Validate and display entered information
labelInfo = licensePlate & " | " & partNumber & " | " & Description & " | " & expirationDate
confirmMsg = "Is the following information correct?" & vbCrLf & vbCrLf & labelInfo
confirmed = MsgBox(confirmMsg, vbYesNo) = vbYes
'Update the label's caption with the user-created string in the Userform
UserForm1.Label3.Caption = labelInfo
' Step 4: Allow correction or confirmation
If Not confirmed Then
' Provide an option to go through the steps again if the information was incorrect
Call CreateAndPrintLabel
Exit Sub
End If
' Store labelInfo in cell B12 on Sheet1
ThisWorkbook.Sheets("Sheet1").Range("B12").Value = labelInfo
' Step 5: Confirm if user wants to print
printLabel = MsgBox("Do you wish to print the label?", vbYesNo)
If printLabel = vbNo Then
Exit Sub ' Exit the macro if user selects "No" for printing
End If
' Step 5.5: Print the label using Brother printer
Set ObjDoc = CreateObject("bpac.Document")
sPath = "C:\Users\55455.buff\Documents\My Labels\Matrix.lbx"
' Open lbx file
If ObjDoc.Open(sPath) Then
' Set text for the entire label
ObjDoc.SetText 0, dataMatrixContent
ObjDoc.GetObject("dataMatrix").text = labelInfo
' Print the label
ObjDoc.StartPrint "", bpoDefault
ObjDoc.PrintOut 1, bpoDefault
ObjDoc.EndPrint
' Close lbx file
ObjDoc.Close
Else
MsgBox "Failed to open label file."
End If
Else
MsgBox "ActiveX Data Matrix control not found on the sheet."
End If
'End If
End Sub
Function ValidatePartNumber(partNumber As String) As Boolean
If CountCharacter(partNumber, "-") = 2 And Right(partNumber, 4) = "-875" Then
ValidatePartNumber = True
Else
ValidatePartNumber = False
End If
End Function
Function CountCharacter(ByVal text As String, ByVal character As String) As Long
CountCharacter = (Len(text) - Len(Replace(text, character, ""))) / Len(character)
End Function
Function IsValidExpirationDate(expirationDate As String) As Boolean
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^\d{4}-\d{2}-\d{2}$"
If regex.Test(expirationDate) Then
Dim currentDate As Date
currentDate = Date
Dim futureDate As Date
futureDate = DateAdd("yyyy", 6, currentDate)
If DateValue(expirationDate) <= futureDate Then
IsValidExpirationDate = True
End If
End If
End Function
Function ValidateSupplierPlateID(supplierPlateID As String) As Boolean
If Len(supplierPlateID) = 8 And Left(supplierPlateID, 2) = "PC" Then
ValidateSupplierPlateID = True
Else
ValidateSupplierPlateID = False
End If
End Function
Sub Print_Label()
Dim bRet As Boolean
Dim sPath As String
Dim ObjDoc As bpac.Document
Set ObjDoc = CreateObject("bpac.Document")
sPath = "C:\Users\giovanni.fontanetta\Documents\My Labels\Matrix.lbx"
'Open lbx file
bRet = ObjDoc.Open(sPath)
If (bRet <> False) Then
' Start Print-Setting
ObjDoc.StartPrint "", bpoDefault
' Ask the user how many copies to print
Dim numCopies As Integer
numCopies = InputBox("Enter the number of copies to print:", "Number of Copies", 1)
For i = 1 To numCopies
ObjDoc.PrintOut 1, bpoDefault
Next i
' Finish Print-Setting and start the printing
ObjDoc.EndPrint
' Close lbx file
ObjDoc.Close
End If
End Sub
答:
1赞
Alok
11/15/2023
#1
我怀疑以下行导致了问题,可能是由于您的工作计算机上未正确安装兄弟打印机驱动程序或相关软件。
Set ObjDoc = CreateObject("bpac.Document")
您可以将一些 Debug.print 或 Msgbox 语句放在代码中的多个位置,以缩小范围并了解代码的哪一部分给出错误。
评论
0赞
St3althPatchin
11/15/2023
在我目前的参考资料库中,我有一个 Brother b-PAC 3.1 类型库。如果我回头看看我个人计算机上的工作 vba,我会看到它的 b-PAC 3.2 类型库。我以前没有注意到这一点。这是否会导致它不起作用?
0赞
Alok
11/15/2023
您可以尝试在 Tools=>Reference 菜单中添加对正确类型库的引用。
0赞
St3althPatchin
11/15/2023
我能够重新下载参考库。还添加了适当的 debug.prints。似乎我无法设置 DataMatrix 文本ObjDoc.GetObject("Data_Matrix").text = labelInfo
0赞
Alok
11/15/2023
您是否在每一行之前输入 Debug.print 以缩小到确切的行,或者您是否怀疑该行是错误出现在该行附近。有时,在没有放置足够的debug.print语句的情况下,我们会精确定位不正确的行。
0赞
St3althPatchin
11/15/2023
我得出这个结论是因为,在我当前代码中的每个更改间隔之后添加 Debug.print 后,我看到在上面的代码之前,我在“即时”窗口中看到我已经为整个标签设置了文本。但是,一旦我到达上面的代码行,我就无法获得debug.print,并且代码出现错误。让我明白我不能将用户设置为数据矩阵的文本labelInfo
1赞
St3althPatchin
11/21/2023
#2
显然,在联系了 Brother 支持,然后使用打印机重新运行代码后,找到了答案。宏之所以能够在我的个人笔记本电脑上工作,但不能在工作笔记本电脑上工作,是因为它是 32 位的。显然,Brother 不支持使用 Excel 打印到 64 位系统上的打印机的功能。这就是工作计算机的运行方式。
在我的每次测试中,我都能够通过在 64 位和 32 位系统上重复打印步骤来证明这一点。每个 32 位都能够从 Excel 接收消息并打印。在 64 位上,打印机可以通过 Windows 进行测试打印,也可以手动打印。但是,从 Excel 接收消息时无法连接。此外,这一点很明显,因为与 32 位相比,允许 Microsoft 程序套件与打印机通信的 Brother 打印机插件实用程序在 64 位计算机上显示为灰色且不可用。
评论
On Error Resume Next
Set ObjDoc = CreateObject("bpac.Document")