PDA

View Full Version : Remove missing reference



kerenlu
05-29-2012, 11:19 PM
In my organization I have users who have Acrobat Writer licence and other who don't. I write the following code in order to remove the missing reference (Mark as MISSING in the reference window:

For i = ThisDocument.VBProject.References.Count To 1 Step-1
Set theRef = ThisDocument.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisDocument.VBProject.References.Remove theRef
End If
Next i

But I am getting the error "object library not registered".
Any Help?

Frosty
05-30-2012, 10:42 AM
What line is erroring on you? Are you using Option Explicit? Do you have the lines
Dim i As Integer
Dim theRef As Object

in your calling code? Does the following work instead?

Sub test()
Dim i As Integer
Dim iCount As Integer
Dim theRef As Object

iCount = ThisDocument.VBProject.References.Count
For i = iCount To 1 Step -1
Set theRef = ThisDocument.VBProject.References(i)
If theRef.IsBroken Then
ThisDocument.VBProject.References.Remove theRef
End If
Next
End Sub

You may also require trusted access to the VBProject object in your Trusted Locations or Macro Security, depending on the version of Word you're working in.

There are other ways to go about this process, incidentally, where you can avoid the compile issues... you could, as an example:
1. Put all of your code which utilizes Acrobat Pro in a separate module
2. Use a function which determines whether acrobat pro is installed the machine (*not* in the Acrobat Pro module)
3. Never call any code contained in the Acrobat Pro module unless your function in #2 returns True.

Since Word compiles entire modules at run-time when any procedure in a module is called, you could effectively eliminate run-time compile errors on the machine which doesn't have Acrobat Pro installed, without worrying about whether the reference is missing or not.

I'm unable to test the effectiveness of the .IsBroken value or the .Remove functionality since I do not have Acrobat Pro and don't know your specific set of critieria... so the code I posted may not help at all. However, the strategy to avoid the compile errors is solid. I've employed that strategy for years where I have a global addin which may contain code only applicable to certain applications installed on some computers.