2017-01-27 147 views
2

在VBA中,我可以看到的PDFCreator三種不同的引用。其中一個(見第二張圖)是本地存儲的軟件版本,並且可以工作。我想使用這個參考。刪除VBA項目引用

另外兩個是對存儲在服務器上的版本的引用,並且它們已損壞(在此階段,我無權重新安裝或刪除它們)。

我的問題是,在選擇所需的參考(見第二幅圖像)並單擊「確定」後,它會重置爲不正確的參考,如第三幅圖所示。

我該如何重寫正在發生的事情並選擇所需的參考或刪除不正確的參考?雖然我無法從服務器上卸載這些版本,但我沒有看到我的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

+2

有趣的是,我認爲這將是更好的嘗試[後期綁定](http://peltiertech.com/Excel/EarlyLateBinding.html)這個錯誤作爲解決方法 – Sgdva

+1

我不明白爲什麼你不能只是刪除破參考? – brettdj

+0

@brettdj是否可以通過非編程方式刪除破損的引用?鑑於它是一次性的,用代碼去除它看起來過於浪費 –

回答

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) 

並且您可以從:

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 
+0

'References.Remove'是否取消引用或完全刪除引用? –