Late binding

A

Angus

I currently have the following code which uses Outlook to send an email:

************
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.Subject = "Subject here"
.BodyFormat = olFormatPlain
.Body = "Hello world."
.Attachments.Add "C:\test.txt"
.Send
End With
**************

I understand that is it possible to change this so that it has "late
binding" which will make it independant of any particular version of Outlook.
I don't know what this means, how do I do it?

Thanks in advance.
 
D

Douglas J. Steele

Go into the References dialog and uncheck whatever Outlook reference you
currently have, and change your code to:


Const olMailItem As Long = 0
Const olFormatPlain As Long = 1

Dim olApp As Object
Dim objMail As Object
Set olApp = CreateObject("Outlook.Application")
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.Subject = "Subject here"
.BodyFormat = olFormatPlain
.Body = "Hello world."
.Attachments.Add "C:\test.txt"
.Send
End With


Note that all constants associated with Outlook (olMailItem and
olFormatPlain) in your code need to have their values assigned. If there are
other constants you've used and you don't know their values, add the
reference back in and go to the Immediate Windows (Ctrl-G). You can type the
name of the constant (preceded with a question mark), then hit Enter, and
the value of the constant will be returned, like:

?olMailItem
0
 

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