2011-05-22 58 views
0

我是新來的VBA,但我試圖讓DYMO LabelWriter在我的VBA應用工作爲Outlook 2010無法(在VBA DYMO SDK對於Outlook 2010)創建的OLE對象

我已經發現了各種代碼示例,但它們都不起作用。他們都返回具有相同的錯誤:

"Unable to create OLE objects"

希望這是一個簡單的錯誤造成的,由於我不習慣用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 
+0

順便說一句:我在Windows 7 64位,並已安裝最新版本的DYMO標籤軟件 – 2011-05-22 14:46:20

+0

如果我修改顯示錯誤Err.Description的代碼,我得到的是:「自動化錯誤」 – 2011-05-22 14:49:26

+0

如果您能告訴我們程序死在哪一行,這將會很有幫助。 – Codo 2011-05-22 17:22:59

回答

0

因爲我不知道DYMO軟件,我只能猜測是什麼問題:

假設DYMO SDK是在 - 處理COM組件(它是作爲DLL而不是EXCE交付的),它必須具有與主機程序相同的位(在你的情況下爲Outlook)。因此,如果您的Outlook是64位版本,那麼COM DLL也必須是64位版本。

另一個潛在的問題可能是軟件安裝不正確。

BTW:你並不需要從VBA引用任何COM組件,因爲該代碼使用後期綁定(所有變量類型的對象)。最好刪除這些參考。

+0

DYMO SDK確實提供COM接口(http://sites.dymo.com/DeveloperProgram/Pages/LW_SDK_Windows.aspx)。我猜測這可能與64位軟件不兼容,但我嘗試使用32位版本的Outlook 2007在Windows Vista 32位計算機上執行代碼。發生同樣的錯誤。如果我打開「On Error Resume Next」,出現以下錯誤:「運行時錯誤429 - activex組件無法創建對象」 – 2011-05-22 18:02:20

+0

我可能在此處取得了進步。所以我試圖在編輯器中給組件添加「DLSSDKCOMLibrary.dll」。該DLL被正確識別,沒有錯誤。但是當我點擊確定並重新打開組件管理器時,dll消失了(甚至沒有被取消選擇),並且當我現在運行代碼時,我得到一個不同的錯誤:「運行時錯誤-2147467261(80004003)自動化錯誤」 – 2011-05-22 19:37:14