【问题标题】:Unable to create OLE object (DYMO SDK in VBA for Outlook 2010)无法创建 OLE 对象(VBA for Outlook 2010 中的 DYMO SDK)
【发布时间】:2011-08-30 12:35:10
【问题描述】:

我是 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

【问题讨论】:

  • 顺便说一句:我在 Windows 7 64 位上,并且已经安装了最新版本的 DYMO 标签软件
  • 如果我修改显示 Err.Description 错误的代码,我得到的只是:“自动化错误”
  • 如果你能告诉我们程序在哪一行死掉会很有帮助。
  • 哦,当然 :) 第 16 行导致问题

标签: vba outlook ms-office ole


【解决方案1】:

由于不了解DYMO软件,只能猜测是什么问题:

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

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

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

【讨论】:

  • DYMO SDK 确实提供了 COM 接口 (sites.dymo.com/DeveloperProgram/Pages/LW_SDK_Windows.aspx)。我猜它可能与 64 位软件不兼容,但我在装有 32 位版本 Outlook 2007 的 Windows Vista 32 位机器上尝试了该代码。发生同样的错误。如果我打开“On Error Resume Next”,我会收到以下错误:“Runtime error 429 - activex component can't create object”
  • 我可能在这里取得了一些进展。所以我尝试在编辑器的组件中添加“DLSSDKCOMLibrary.dll”。 DLL 被正确识别,没有错误。但是当我单击确定并重新打开组件管理器时,dll 消失了(甚至没有取消选择),当我现在运行代码时,我得到一个不同的错误:“运行时错误 -2147467261(80004003)自动化错误”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-29
  • 2017-11-17
  • 2011-02-13
  • 1970-01-01
  • 2012-10-14
相关资源
最近更新 更多