excel 打印脚本中的对象变量错误

p8h8hvxi  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(125)

我最近将Excel vba项目从我的个人计算机移动到我的工作计算机。此项目旨在使用兄弟打印机连接和打印。我一直收到运行时错误'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

字符串

qfe3c7zg

qfe3c7zg1#

我怀疑下面这一行是导致问题可能是由于不正确的安装兄弟打印机驱动程序或相关软件在您的工作计算机上。

Set ObjDoc = CreateObject("bpac.Document")

字符串
您可以在代码中的多个位置放置一些full.print或Msgbox语句,以缩小范围并了解代码的哪一部分产生错误。

相关问题