How to remove missing references via script in Excel VBA?

我只是一个虾纸丫 提交于 2019-12-20 07:32:16

问题


I have excel that is use by many users. Some of them do not have installed all software's and therefore missing references. I am, trying to have script that remove all references that can not be found on C: drive.

I have this code in /Microsoft Excel Objects / ThisWorkbook but is not fully working. Anyone could help me with this.

Sub TestRef()
Dim REF As VBIDE.Reference
Dim WB As Workbook
Set WB = ThisWorkbook
For Each REF In WB.VBProject.References
    If StrComp(Left(REF.FullPath, 1), "C", vbTextCompare) <> 0 Then
        DelRef
        Debug.Print REF.Name, REF.Description, REF.FullPath
    End If
Next REF
End Sub

Sub DelRef()
Dim REF As VBIDE.Reference
Dim WB As Workbook
Set WB = ThisWorkbook
With ThisWorkbook.VBProject.References
    .Remove .Item("MSForms")
End With    
End Sub   

Missing Referece Image


回答1:


Not with your variables names, but something I wrote a while back and I've been using for a while to remove "Missing" references :

Dim theRef As Variant, i As Long

' loop through all References in VB Project
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
    Set theRef = ThisWorkbook.VBProject.References.Item(i)

    ' if reference is "Missing" >> remove it to avoid error message
    If theRef.isbroken = True Then
        ThisWorkbook.VBProject.References.Remove theRef
    End If

    ' just for Debug
    ' Debug.Print theRef.Description & ";" & theRef.FullPath & ";" & theRef.isbroken & vbCr
Next i


来源:https://stackoverflow.com/questions/43675942/how-to-remove-missing-references-via-script-in-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!