如何在VBA中以编程方式删除引荐?

时间:2019-09-16 13:42:01

标签: excel vba

我在Citrix应用程序上用Excel VBA 2016版本编写了一些代码,当我在Citrix应用程序上运行它时,它工作正常,但是当我在Excel 2010版本的普通桌面上运行此宏时,我却无法运行,面临一些参考问题,一旦我清除了该参考,便可以在普通桌面上运行它。所以这里我的问题是如何编写代码以明确检查该引用。 因此,我需要VBA代码来清除“缺少:ALTEntityPicker 1.0类型库”支票

我已经为用户提供了此代码以供删除参考,但是它不起作用。

Sub DeleteRef(RefName)
    Dim ref As Reference

    'You need a reference to remove '
    Set ref = References("Missing: ALTEntityPicker 1.0 Type Library")
    References.Remove ref
End Sub 

3 个答案:

答案 0 :(得分:1)

不可能以编程方式删除 A MISSING/损坏的引用在 MISSING 发生后,只能在它发生之前或在它发生之后手动删除。 大多数 MISSING/破坏引用的情况是因为类型库之前从未在该系统上注册过。

预防:通过在 Workbook_BeforeClose 事件中删除任何有问题的引用并在 Workbook_Open 事件中重新添加它,预先避免丢失/损坏的引用。在示例中,'Selenium' 是导致错误的引用,因此我使用 Workbook_BeforeClose 将其删除,然后在 Workbook_Open 上重新添加。如果不能添加就不会添加,也不会出现MISSING,如果可以添加就添加。

Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed?

<块引用>
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Cancel = True Then Exit Sub
    RemoveReference
End Sub
<块引用>
Private Sub Workbook_Open()
 AddReferences
End Sub
Public Sub RemoveReference()
On Error GoTo EH
    Dim RefName As String
    Dim ref As Reference
    RefName = "Selenium"
     
    Set ref = ThisWorkbook.VBProject.References(RefName)
    ThisWorkbook.VBProject.References.Remove ref
    
Exit Sub
EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 9
            MsgBox "The reference is already removed"
        Exit Sub
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
       Case Else
         'An unknown error was encountered
            MsgBox "Error in 'RemoveReference'" & vbCrLf & vbCrLf & Err.Description
    End Select
End Sub

Public Sub AddReferences()
    Dim wbk As Workbook
    Set wbk = ActiveWorkbook

    AddRef wbk, "{0277FC34-FD1B-4616-BB19-A9AABCAF2A70}", "Selenium"
End Sub

Sub AddRef(wbk As Workbook, sGuid As String, sRefName As String)
    Dim i As Byte
    On Error GoTo EH
    With wbk.VBProject.References
        For i = 1 To .Count
            If .item(i).Name = sRefName Then
               Exit For
            End If
        Next i
        If i > .Count Then
           .AddFromGuid sGuid, 0, 0 ' 0,0 should pick the latest version installed on the computer
        End If
    End With

Exit Sub

EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
    Case Else
         'An unknown error was encountered
            MsgBox "Error in 'AddRef'" & vbCrLf & vbCrLf & Err.Description
    End Select
End Sub

Public Sub ExistingRefs()
 Dim i As Byte
 On Error GoTo EH
      With Application.ThisWorkbook.VBProject.References
        For i = 1 To .Count
            Debug.Print "    AddRef wbk, """ & .item(i).GUID & """, """ & .item(i).Name & """"
        Next i
    End With
    
Exit Sub
EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
    Case Else
         'An unknown error was encountered
            MsgBox "Error in 'ExistingRefs'" & vbCrLf & Err.Description
    End Select
End Sub

答案 1 :(得分:0)

尝试一下

Sub DeleteRef(RefName) 
    Dim ref As Reference

    'You need a reference to remove '
    Set ref = References("Missing: ALTEntityPicker 1.0 Type Library")
    vbProj.References.Remove ref
End Sub

答案 2 :(得分:0)

尝试一下:

重要提示:如果以前从未在该系统上注册类型库,则该引用不适用于残破的引用。

https://social.msdn.microsoft.com/Forums/lync/en-US/ea27f90a-d91d-44f1-9a3c-71ab858601ff/excel-remove-reference-run-time-error-2147319779?forum=exceldev

''以编程方式删除特定的或损坏的引用

Sub UnloadReferences()

On Error Resume Next
Dim i As Long
Dim ref As Variant
   Set ref = ThisWorkbook.VBProject.References
       For i = 1 To ref.Count
            If ref.item(i).IsBroken = True Then
               ref.Remove (ref.item(i))
            End If
        
''Example: Remove Reference "Selenium Type Library" (even if it is not missing on our PC)

'            If ref.item(i).Name = "Selenium" Then
'               ref.Remove (ref.item(i))
'            End If

        Next i
End Sub