没有响应或挂起excel宏2010

时间:2014-05-29 02:04:21

标签: excel vba

帮助,我在excel宏中有这个代码。当我执行excel程序时它遇到挂起它在excel 2000中运行完美但在excel 2010中出错。提前感谢...

Sub AddReference()
Macro purpose:  To add a reference to the project using the GUID for the`
     'reference library     
    Dim strGUID As String, theRef As Variant, i As Long     
     'Update the GUID you need below.
    strGUID = "{00020813-0000-0000-C000-000000000046}"     
     'Set to continue in case of error
    On Error Resume Next     
     'Remove any missing references    
    For i = ThisWorkbook.VBProject.References.Count To 1 Step -1     
        Set theRef = ThisWorkbook.VBProject.References.Item(i)        
        If theRef.isbroken = True Then        
            ThisWorkbook.VBProject.References.Remove theRef
        End If         
    Next i     
     'Clear any errors so that error trapping for GUID additions can be evaluated
    Err.Clear     
     'Add the reference
    ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:=strGUID, Major:=1, Minor:=0     
     'If an error was encountered, inform the user
    Select Case Err.Number
    Case Is = 32813
         'Reference already in use.  No action necessary
    Case Is = vbNullString
         'Reference added without issue
    Case Else
         'An unknown error was encountered, so alert the user
        MsgBox "A problem was encountered trying to" & vbNewLine _
        & "add or remove a reference in this file" & vbNewLine & "Please check the " _
        & "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
    End Select
    On Error GoTo 0
End Sub

1 个答案:

答案 0 :(得分:0)

此GUID引用**主互操作程序集",它是所有办公室版本(2003年,2007年,2010年我可以确认)的相同GUID。

我猜您的计算机没有安装它/正确的版本。

您需要为Office 2010安装它。Microsoft Office 2010: Primary Interop Assemblies Redistributable

如果您的计算机上安装了多个Office版本,请告诉我们。

您很可能需要更改代码:

Major:=1, Minor:=0(办公室2003)至Major:=1, Minor:=7(办公室2010)