Excel自动化错误#1004对象'〜'的方法'〜'失败

时间:2016-05-06 20:33:59

标签: excel vba vb6 automation add-in

使用VB6进行一些Excel自动化我有这个代码,这个代码在过去的5年里一直运作良好。它基本上遍历加载项,如果它检测到某个加载项,则它会向用户发出一条消息。

        Public xlapp As Excel.Application
        Set xlapp = GetObject(, "Excel.Application")

        Dim CurrAddin As Office.COMAddIn

        For Each CurrAddin In xlApp.COMAddIns
2           LogMe "==>COM Add-In: " & CurrAddin.Description

            'detect the PowerPivot add-in
3           If CurrAddin.Description = "PowerPivot for Excel" And CurrAddin.progID = "Microsoft.AnalysisServices.Modeler.FieldList" Then
4               LogMe "==>COM Add-In 'PowerPivot for Excel is set to load=" & CurrAddin.Connect

5               If CurrAddin.Connect = True Then
6                   MsgBox "The PowerPivot Add-in for Excel is installed and set to load.", vbExclamation
                End If
            End If
9       Next CurrAddin

Error in MyAddInEnumerator
1004
Method '~' of object '~' failed
Line: 2

在此例程上放置错误捕获程序后,调试说它发生在第2行。交互式调试显示这实际上在上面的行中。 CurrAddin似乎已经初始化,因此问题出在xlApp.COMAddIns上。

我有这个应用程序在许多机器上运行,具有相同版本的32位Excel 2010.这些机器运行Win7x86和Win7x64,除了一个运行良好,没有错误。我无法弄清楚为什么这台机器会使用“COMAddIns”对象进行轰炸。

1 个答案:

答案 0 :(得分:0)

在访问对象之前尝试测试对象:

For Each CurrAddin In xlApp.COMAddIns
    'Add this line below and the End If
    If Not IsNull(CurrAddin) Then
        LogMe "==>COM Add-In: " & CurrAddin.Description

        'detect the PowerPivot add-in
        If CurrAddin.Description = "PowerPivot for Excel" And CurrAddin.progID = "Microsoft.AnalysisServices.Modeler.FieldList" Then
            LogMe "==>COM Add-In 'PowerPivot for Excel is set to load=" & CurrAddin.Connect

            If CurrAddin.Connect = True Then
                MsgBox "The PowerPivot Add-in for Excel is installed and set to load.", vbExclamation
            End If
        End If
    End If
Next CurrAddin

空引用或未声明的对象可能导致1004错误,如在其他一些SO文章中那样:

VBA Excel: Run-time error '1004' Method 'Range' of object'_Worksheet' faile

Broken Excel VBA Macro Run-time error '1004': Method 'Range' of object '_Global' failed

Error '1004' Method 'Range' of object'_Global failed

HTH。这可能仍会检测到 do 有加载项。否则,这可能是重新安装问题,如下所示:http://www.xtremevbtalk.com/996711-post4.html