Outlook Automation - Defining Data Types

J

JimP

I have a module that uses Outlook automation such that I need to define the
following,

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

This works fine when Outlook is installed and there is a reference to the
"Microsoft Outlook 11.0 Object Library". However, there is a compile error
when the library is not found (Outlook is not installed). How can I
accommodate both situations, without errors?
 
D

Douglas J. Steele

Use Late Binding.

1. Don't set a reference to Outlook.

2. Change the declarations to

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object

3. If you've got code like

Set objOutlook = New Outlook.Application

change it to

Set objOutlook = CreateObject("Outlook.Application")

4. Ensure that you've defined values for any Outlook constants you're using.
For example,. if you've got code like

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

you need either to include

Const olMailItem As Long = 0

or else change the code to

Set objOutlookMsg = objOutlook.CreateItem(0)

This will be the most time consuming part of the conversion! Make sureyou've
got Access set to require declaration of all variables (It's an option under
Tools | Options when you're in the VB Editor). Assuming you've got that
setting set, remove the reference and compile your application (under the
Debug menu in the VB Editor). The compilation will stop at each undefined
constant.

Tony Toews has an introduction to the subject of Late Binding at
http://www.granite.ab.ca/access/latebinding.htm
 
J

JimP

Thank you.


Douglas J. Steele said:
Use Late Binding.

1. Don't set a reference to Outlook.

2. Change the declarations to

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objOutlookRecip As Object
Dim objOutlookAttach As Object

3. If you've got code like

Set objOutlook = New Outlook.Application

change it to

Set objOutlook = CreateObject("Outlook.Application")

4. Ensure that you've defined values for any Outlook constants you're
using. For example,. if you've got code like

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

you need either to include

Const olMailItem As Long = 0

or else change the code to

Set objOutlookMsg = objOutlook.CreateItem(0)

This will be the most time consuming part of the conversion! Make
sureyou've got Access set to require declaration of all variables (It's an
option under Tools | Options when you're in the VB Editor). Assuming
you've got that setting set, remove the reference and compile your
application (under the Debug menu in the VB Editor). The compilation will
stop at each undefined constant.

Tony Toews has an introduction to the subject of Late Binding at
http://www.granite.ab.ca/access/latebinding.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top