Late binding code

J

John

Hi

I have the below code to send an email from within MS Access using Outlook.
What would be the late binding version of this code?

Many Thanks

Regards


Dim OutlookApp As Outlook.Application
Dim ns As Outlook.NameSpace
Dim EM As Outlook.MailItem

Set OutlookApp = New Outlook.Application

Set ns = OutlookApp.GetNamespace("MAPI")
ns.Logon

Set EM = OutlookApp.CreateItem(olMailItem)

With EM
.To = "(e-mail address removed)"
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Set EM = Nothing
 
N

Norman Yuan

Dim OutlookApp As Object
Dim EM As Object

On Error Resume Next
Set OutlookApp = GetObject(,"Outlook.Application")
If Err.Number<>0 Then
Err.Clear
Set OutlookApp=CreateObject("Outlook.Application")
If Err.Number<>0 Then
MsgBox "Cannot get Outlook"
Exit Sub
End If
End If

OutlookApp.GetNamespace("MAPI").Logon

Set EM = OutlookApp.CreateItem(olMailItem)

With EM
.To = "(e-mail address removed)"
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Of course you'll remove reference to Outlook object library and
 
D

Douglas J. Steele

Not quite. olMailItem is defined in the Outlook library, so you need to
provide a value for the constant:

Set EM = OutlookApp.CreateItem(0)

or

Const olMailItem As Long = 0

Set EM = OutlookApp.CreateItem(olMailItem)
 
S

Stefan Hoffmann

hi Norman,

Norman said:
On Error Resume Next
Set OutlookApp = GetObject(,"Outlook.Application")
If Err.Number<>0 Then
Err.Clear
Set OutlookApp=CreateObject("Outlook.Application")
If Err.Number<>0 Then
MsgBox "Cannot get Outlook"
Exit Sub
End If
End If
Due to the nature of Outlook, it is a single instance application, you
don't need the error trapping. The CreateObject will return the existing
instance or start a new one.


mfG
--> stefan <--
 
K

Ken Slovak - [MVP - Outlook]

But that way of doing things does let you know if you tapped into an
existing instance of Outlook or created a new instance. Very useful if you
want to know whether or not to terminate the Outlook process when your code
is finished.
 
N

Norman Yuan

You are right, I missed olMailItem.

Douglas J. Steele said:
Not quite. olMailItem is defined in the Outlook library, so you need to
provide a value for the constant:

Set EM = OutlookApp.CreateItem(0)

or

Const olMailItem As Long = 0

Set EM = OutlookApp.CreateItem(olMailItem)
 

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