无法为 ActiveX 组件创建对象

Object can't be created for ActiveX component

提问人:St3althPatchin 提问时间:9/21/2023 最后编辑:St3althPatchin 更新时间:10/17/2023 访问量:45

问:

我创建了一个 excel 宏工具,它可以通过询问用户每个参数来制作数据矩阵标签,然后一旦标签的整个字符串被制作出来,它就会被发送到兄弟PT9800PCN打印机。在解决了我遇到的许多问题之后,我终于能够让它工作。但是,我需要将其部署到另一台计算机。通过传输打印标签所需的所有文件和驱动程序,我似乎无法让宏与打印机一起使用。

每次我从用户那里收集所有数据的过程时,我都会继续收到此错误,该错误不允许我正常发送到打印机。Excel vb 继续突出显示我的行:.指出问题是运行时错误 429。我检查以确保我提取了原始机器上所有必要的引用和库,并确保我拥有 Brother 打印机所需的驱动程序。在必须容纳此宏的计算机上,我什至卸载并重新安装了 P-touch 软件和打印机驱动程序。这也适用于参考库。然而,我仍在努力寻找合适的解决方案。Set ObjDoc = CreateObject("bpac.Document")

enter image description here

我在网上查找了Microsoft对运行时错误的官方描述,以及运行和查看我在原始笔记本电脑上设置的参考库。我也在 StackOverflow 上查看了可能对我有帮助的类似问题。具体来说,这篇文章在这里:错误 429。Active X 组件无法创建对象。不幸的是,我仍然没有找到可能导致问题的原因。希望也许通过发布我的问题,也许可以有一些希望解决它。

Public Sub CreateAndPrintLabel()
    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\giovanni.fontanetta\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
Excel VBA ActiveX VBA7 兄弟-BPAC

评论


答: 暂无答案