无法创建OLE对象(适用于Outlook 2010的VBA中的DYMO SDK)

无法创建OLE对象(适用于Outlook 2010的VBA中的DYMO SDK),vba,outlook,ms-office,ole,Vba,Outlook,Ms Office,Ole,我是VBA新手,但我正在尝试让DYMO LabelWriter在我的Outlook 2010 VBA应用程序中工作 我发现了各种代码示例,但它们都不起作用。它们都返回相同的错误: 无法创建OLE对象 希望这是一个简单的错误造成的,因为我不习惯使用VBA 我已经在VBA编辑器中启用了名称中带有DYMO的每个引用 下面是代码示例: Sub PrintLabels() 'Rembo wrote this routine - http://scriptorium.serve-it.nl 'This ro

我是VBA新手,但我正在尝试让DYMO LabelWriter在我的Outlook 2010 VBA应用程序中工作

我发现了各种代码示例,但它们都不起作用。它们都返回相同的错误:

无法创建OLE对象

希望这是一个简单的错误造成的,因为我不习惯使用VBA

我已经在VBA编辑器中启用了名称中带有DYMO的每个引用

下面是代码示例:

Sub PrintLabels()
'Rembo wrote this routine - http://scriptorium.serve-it.nl
'This routine prints Excel data on sticker by using a template and the Dymo high level COM.
'It assumes you have a Dymo labelwriter printer and you have created a label that will
'serve as a template. On the label are two'text objects named OText1 and OText2.
'As a data source we assume text in cells B2:B5 and C2:C5 on your the first worksheet but
'obviously you can use any data source you like.

    Dim myDymo As Object
    Dim myLabel As Object
    Dim sPrinters As String
    Dim arrPrinters() As String
    Dim i As Long, i2 As Long, iStart As Long

    On Error Resume Next
    Set myDymo = CreateObject("Dymo.DymoAddIn")
    Set myLabel = CreateObject("Dymo.DymoLabels")
    If (myDymo Is Nothing) Or (myLabel Is Nothing) Then
        MsgBox "Unable to create OLE objects"
        Exit Sub
    End If

    'Check forDymo printer(s)
    'If there is one proceed and store the printernames in a variable, else quit
    sPrinters = myDymo.GetDymoPrinters()
    If sPrinters = "" Then
        Exit Sub
    Else
        i2 = 0
        iStart = 1
        For i = 1 To Len(sPrinters)
            If Mid(sPrinters, i, 1) = "|" Then
                i2 = i2 + 1
                ReDim Preserve arrPrinters(i2 + 1)
                arrPrinters(i2) = Mid(sPrinters, iStart, i - iStart)
                iStart = i + 1
            End If
        Next i
    End If

    'Store the current default printer and select the Dymprinter of your choice
    sDefaultPrinter = Application.ActivePrinter
    With myDymo
        '0 is first Dymo printer, you could use the printername instead: SelectPrinter "YourPrintername"
        .SelectPrinter arrPrinters(0)
    End With

    'Open the label template
    myLabel = myDymo.Open("C:\SomeFolder\LabelName.LWL")

    For i = 3 To 5
        'Give text objects OText1 and OText2 on the label a value
        With myLabel
            .SetField "OText1", Worksheets(1).Range("B" & i).Value
            .SetField "OText2", Worksheets(1).Range("C" & i).Value
        End With

        'Print the label
        With myDymo
            .StartPrintJob  'Only used for Turbo 400 model and higher for print optimizing, you may omit it
            .Print 2, False ' Print 2 copies
            .EndPrintJob    'Only used for Turbo 400 model and higher for print optimizing, you may omit it
        End With
    Next i

    'Make sure the default printer is selected again
    Application.ActivePrinter = sDefaultPrinter

    'Clean up
    Set myLabel = Nothing
    Set myDymo = Nothing
End Sub

由于我不知道DYMO软件,我只能猜测问题出在哪里:

假设DYMO SDK是一个进程内COM组件,它是作为DLL而不是EXE交付的,它必须与您的主机程序Outlook具有相同的位。因此,如果Outlook是64位版本,那么COM DLL也必须是64位版本

另一个潜在问题可能是软件安装不正确


顺便说一句:您不需要从VBA引用任何COM组件,因为代码使用后期绑定,所有变量都是Object类型。最好删除这些引用。

顺便说一句:我使用的是64位Windows 7,安装了最新版本的DYMO label软件,如果我修改代码,它会显示错误。错误描述我得到的只是:自动化错误。如果你能告诉我们程序在哪一行死亡,那会很有帮助。哦,当然:第16行导致了错误问题DYMO SDK确实提供了COM接口。我猜这可能与64位软件不兼容,但我在32位版本Outlook 2007的Windows Vista 32位计算机上尝试了这段代码。同样的错误也会发生。如果打开“下一步错误恢复”,则会出现以下错误:运行时错误429-activex组件无法创建对象我可能在此处取得了som进展。因此,我尝试将DLSSDKCOMLibrary.dll添加到编辑器中的组件中。DLL被正确识别,没有错误。但是,当我单击OK并重新打开components manager时,dll甚至没有被取消选择,当我现在运行代码时,我得到一个不同的错误:运行时错误-2147467261 80004003自动化错误