问题
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