'A mettre dans ThisWorkbook
'Attention Cochez Accès Approuvé au modèle d'objet du projet VBA dans les options. Choisir:
'Fichier \Options \Centre Gestion de la confidentialité \Paramètres du centre de la confidentialité \Paramètre des Macros
Private Sub Workbook_BeforeClose(Cancel As Boolean)
EnlèveRéférence_Outlook
End Sub
Private Sub Workbook_Open()
AjouteRéférence_Outlook
End Sub
'A Mettre dans un module standard
Sub AjouteRéférence_Outlook()
On Error Resume Next
'MsgBox Application.Version
'MsgBox Dir("C:\Program Files\Microsoft Office\Office14\MSOUTL.OLB")
'MsgBox Dir("C:\Program Files (x86)\Microsoft Office\Office14\MSOUTL.OLB")
If Application.Version = "14.0" And Dir("C:\Program Files (x86)\Microsoft Office\Office14\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files (x86)\Microsoft Office\Office14\MSOUTL.OLB"
If Application.Version = "14.0" And Dir("C:\Program Files\Microsoft Office\Office14\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office14\MSOUTL.OLB"
If Application.Version = "15.0" And Dir("C:\Program Files(x86)\Microsoft Office\Office15\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files (x86)\Microsoft Office\Office15\MSOUTL.OLB"
If Application.Version = "15.0" And Dir("C:\Program Files\Microsoft Office\Office15\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office15\MSOUTL.OLB"
If Application.Version = "16.0" And Dir("C:\Program Files (x86)\Microsoft Office\Office16\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files (x86)\Microsoft Office\Office16\MSOUTL.OLB"
If Application.Version = "16.0" And Dir("C:\Program Files\Microsoft Office\Office16\MSOUTL.OLB") = "MSOUTL.OLB" Then ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office16\MSOUTL.OLB"
End Sub
Sub EnlèveRéférence_Outlook()
Dim Ref As Object
'Efface la Référence Outlook
For Each Ref In ThisWorkbook.VBProject.References
'If Ref.IsBroken = True Then ThisWorkbook.VBProject.References.Remove Ref
If Ref.Name = "Outlook" Then ThisWorkbook.VBProject.References.Remove Ref
'MsgBox Ref.Name
Next Ref
End Sub
Sub TestRefBroken()
Dim Ref As Object
'Efface les références "Broken" ou "Manquantes"
For Each Ref In ThisWorkbook.VBProject.References
If Ref.IsBroken = True Then ThisWorkbook.VBProject.References.Remove Ref
'MsgBox Ref.Name
Next Ref
End Sub