2017-01-27 148 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'是否取消引用或完全删除引用? –