删除VBA项目参考

时间:2017-01-27 03:43:54

标签: vba excel-vba excel

在VBA中,我可以看到PDFCreator的三个不同参考。其中一个(参见第二个图像)是本地存储的软件版本,它可以工作。我想使用这个参考。

另外两个是对存储在服务器上的版本的引用,它们被破坏了(在这个阶段,我没有权限重新安装或删除它们)。

我的问题是,在选择了所需的参考(参见第二张图片)并点击“确定”后,它会重置为不正确的参考,如第三张图片所示。

如何覆盖正在进行的操作并选择所需的引用或删除不正确的引用?虽然我无法从服务器卸载这些版本,但我认为我的Excel无需引用它们。它们可以从列表中删除吗?

图1:VBA项目参考的默认状态(未选择PDFCreator)

Here's what it looks like before adding a reference

图2:选择正确的PDFCreator版本 Selecting correct (local) PDFCreator reference

图3:重新打开菜单显示选择了错误的PDFCreator版本 Hitting ok and re-opening References shows that it's changed to an incorrect (on server) PDFCreator reference

3 个答案:

答案 0 :(得分:0)

您可能会遇到以下情况......

删除损坏的引用:

Private Sub RemoveBrokenReferences(ByVal Book As Workbook)

'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Dim oRefS As Object, oRef As Object
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Set oRefS = Book.VBProject.References
For Each oRef In oRefS
    If oRef.IsBroken Then
        Call oRefS.Remove(oRef)
    End If
Next

End Sub

删除特定参考:

使用类似:

Call ActiveWorkbook.VBProject.References.Remove(oReference)

你可以从

获得oReference
Private Function GetReferenceFromPath(ByVal FilePathName As String) As Object

'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Dim oFs As Object, oReferenceS As Object, oReference As Object
Dim sFileName  As String, sRefFileName As String
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Set oFs = Interaction.CreateObject("Scripting.FileSystemObject")

sFileName = oFs.GetFileName(FilePathName)
Set oReferenceS = ActiveWorkbook.VBProject.References
For Each oReference In oReferenceS
    sRefFileName = oFs.GetFileName(oReference.FullPath)
    If StrComp(sFileName, sRefFileName, vbTextCompare) = 0 Then
        Set GetReferenceFromPath = oReference
    End If
Next

End Function

答案 1 :(得分:0)

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

P.S 无法以编程方式删除 A MISSING/损坏的引用在 MISSING 发生后,只能在它发生之前或在它发生之后手动删除。大多数丢失/损坏引用的情况是因为类型库之前从未在该系统上注册过。 见How to Remove Reference programmatically?

答案 2 :(得分:-1)

当我卸载 Flash 时,我遇到了大量 Excel 电子表格的参考问题(由于某种未知原因,我将其包含为参考)。 我解决了以下问题:

要小心,因为这涉及到注册表黑客,而且很复杂。 在黑客入侵之前备份注册表。

我编写了 VBA 代码来查找损坏引用的 Guid。 我使用 Regedit 插入了一个 DUMMY TypeLib 条目,如下所示:

D27CDB6B-AE6D-11CF-96B8-444553540000 是损坏的参考指南。

HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000} HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0 Adob​​e Acrobat 7.0 浏览器控件类型库 1.0 HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\0 HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\0\win32 C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\FLAGS 0 HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\HELPDIR C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\

以上内容基于另一个 TypeLib 条目。

然后我编写了 VBA 代码来依次读取每个 Reference.Guid,如果 Guid 匹配 {D27CDB6B-AE6D-11CF-96B8-444553540000} 则使用 References.Remove Reference 删除引用。

执行此操作的代码在论坛上随处可见,所以我不会在这里重复。

修改所有受影响的工作簿后,我恢复了保存的注册表。

希望这对你有用。

要小心,因为这涉及到注册表黑客,而且很复杂。 在黑客入侵之前备份注册表。

相关问题