Hello,
I have an excel xlsm file which includes Microsoft outlook reference.
The file is opened in excel 2007 and excel 2010.
When the file is opened in Excel 2007 after it is opened in Excel 2010
I get a reference error message and a missing reference.
I wish to dynamic update the reference - remove the missing outlook reference and add the correct one.
I use the following code. If the reference is missing it adds it correctly.
My problem: when there is a missing reference the code can not remove it and I get an error.
Can you help me ?
Thank you for your help
Sub LoadOutlookReferences()
'load Outlook object library
On Error Resume Next
'When using AddFromGUID, you can use zero for the major
'and minor versions to pick the latest.
'adding VBE object library:
ActiveWorkbook.VBProject.References.AddFromGuid _
"{0002E157-0000-0000-C000-000000000046}", 0, 0
'Adding outlook object library:
Application.VBE.ActiveVBProject.References _
.AddFromGuid "{00062FFF-0000-0000-C000-000000000046}", 0, 0
On Error GoTo 0
End Sub
Sub References_RemoveMissing()
'Macro purpose: To remove missing references from the VBE
Dim theRef As Variant, i As Long
On Error Resume Next
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(i)
If theRef.IsBroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next i
If Err <> 0 Then
MsgBox "A missing reference has been encountered!" _
& "You will need to remove the reference manually.", _
vbCritical, "Unable To Remove Missing Reference"
End If
On Error GoTo 0
End Sub
Bookmarks