Remove a VBA Project Reference

2019-07-11 01:28发布

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.

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?


Image 1: Default state of the VBA Project References (PDFCreator not selected)

Here's what it looks like before adding a reference

Image 2: Selecting the correct PDFCreator version Selecting correct (local) PDFCreator reference

Image 3: Re-opening the menu shows that the incorrect PDFCreator version is selected Hitting ok and re-opening References shows that it's changed to an incorrect (on server) PDFCreator reference

1条回答
何必那么认真
2楼-- · 2019-07-11 02:21

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:

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
查看更多
登录 后发表回答