Microsoft Access Office VB VBA Help and Examples

Adding a reference in Code

If you have a database that uses a Microsoft Outlook reference you may encounter problems because the clients using the database do not all have the same version of outlook. To overcome this you can add and remove references on the fly. This piece of code shows you how.

Paste the following three subs into a new module.

Private Sub RemoveOutlook()
     On Error Resume Next
     References.Remove References("Outlook")
End Sub

Private Sub TryAddAll()
     On Error Resume Next
     References.AddFromFile "C:\Program Files\Microsoft Office\Office\msoutl8.olb"
End Sub

Private Sub ListRefs()
     Dim Ref As Reference
     For Each Ref In Application.References
          Debug.Print Ref.Name
          Debug.Print Ref.FullPath
End Sub
  • RemoveOutlook will take any Outlook reference currently installed in your database and remove it.
  • TryAddAll will try to add the references listed. I have shown you the line to add the outlook reference for Outlook 97.  You would need to add extra lines for all versions you think you need.
  • ListRefs will list the name and path of all references currently in your system. Use this to find out the names of the various outlook olb files.

If you wanted to be a little cleverer you could look for and check if the file exists before trying to add that reference using the dir function.

The code and application content of this site is copyright of Smiley I.T. and as such reproduction in any form which is for commercial use requires the permission of the Webmaster. Any use of this code for non-commercial use only requires a link or comment back to the original page you took the code from.