开发者

Unable to create OLE object (DYMO SDK in VBA for Outlook 2010)

开发者 https://www.devze.com 2023-03-07 08:19 出处:网络
I\'m new to VBA but I\'m trying to get a DYMO LabelWriter to work in my VBA application for Outlook 2010.

I'm new to VBA but I'm trying to get a DYMO LabelWriter to work in my VBA application for Outlook 2010.

I've found various code samples but neither of them works. They all return with the same error:

"Unable to create OLE objects"

Hopefully this is caused by a simple mistake due to the fact that I'm not used to working with VBA.

I've enabled every single reference in the VBA Editor that have something with "DYMO" in the name.

Here an example of the code:

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


Since I don't know the DYMO software, I can only guess what the problem is:

Assuming that the DYMO SDK is an in-process COM component (it is delivered as a DLL as opposed to a EXCE), it has to have the same bitness as the host program (Outlook in your case). So if your Outlook is a 64 bit version, then the COM DLL must also be a 64 bit version.

Another potential problem could be that the software isn't properly installed.

BTW: You don't need to reference any COM components from VBA since the code uses late binding (all variables are of type Object). It's better to remove those refrences.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号