删除 VBA 项目引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41886667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove a VBA Project Reference
提问by Sam Woolerton
In VBA I can see three different references for PDFCreator. One of them (see the second image) is a version of the software stored locally, and it works. I'd like to use this reference.
在 VBA 中,我可以看到 PDFCreator 的三个不同参考。其中之一(见第二张图片)是本地存储的软件版本,它可以工作。我想使用这个参考。
The other two are references to versions stored on a server, and they're broken (at this stage, I don't have permission to reinstall or delete them).
另外两个是对存储在服务器上的版本的引用,它们已损坏(在此阶段,我无权重新安装或删除它们)。
My problem is that after selecting the desired reference (see the second image) and clicking 'Ok', it resets to an incorrect reference, as shown in the third image.
我的问题是,在选择所需的参考(参见第二张图片)并单击“确定”后,它会重置为不正确的参考,如第三张图片所示。
How can I either override whatever's going on and select the desired reference or remove the incorrect references? While I'm not able to uninstall these versions from the server, I see no reason that my Excel would need to reference them. Can they just be removed from the list?
我怎样才能覆盖正在发生的一切并选择所需的参考或删除不正确的参考?虽然我无法从服务器卸载这些版本,但我认为我的 Excel 没有理由需要引用它们。他们可以从列表中删除吗?
Image 1: Default state of the VBA Project References (PDFCreator not selected)
图 1:VBA 项目参考的默认状态(未选择 PDFCreator)
Image 2: Selecting the correct PDFCreator version
Image 3: Re-opening the menu shows that the incorrect PDFCreator version is selected
回答by PaulG
You might be able to something like the following...
您可能可以执行以下操作...
To Remove broken references:
删除损坏的引用:
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
To Remove a specific reference:
删除特定引用:
Use something like:
使用类似的东西:
Call ActiveWorkbook.VBProject.References.Remove(oReference)
and you can get the oReference from:
您可以从以下位置获取 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