Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Outlook and VBA (http://www.outlookbanter.com/outlook-vba/)
-   -   Late binding code (http://www.outlookbanter.com/outlook-vba/71826-late-binding-code.html)

John[_11_] May 12th 08 11:58 PM

Late binding code
 
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 = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Set EM = Nothing



Norman Yuan May 13th 08 12:24 AM

Late binding code
 
Dim OutlookApp As Object
Dim EM As Object

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

OutlookApp.GetNamespace("MAPI").Logon

Set EM = OutlookApp.CreateItem(olMailItem)

With EM
.To = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

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


"John" wrote in message
...
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 = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Set EM = Nothing



Douglas J. Steele May 13th 08 01:17 AM

Late binding code
 
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)


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Norman Yuan" wrote in message
...
Dim OutlookApp As Object
Dim EM As Object

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

OutlookApp.GetNamespace("MAPI").Logon

Set EM = OutlookApp.CreateItem(olMailItem)

With EM
.To = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

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


"John" wrote in message
...
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 = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Set EM = Nothing





Stefan Hoffmann May 13th 08 09:17 AM

Late binding code
 
hi Norman,

Norman Yuan wrote:
On Error Resume Next
Set OutlookApp = GetObject(,"Outlook.Application")
If Err.Number0 Then
Err.Clear
Set OutlookApp=CreateObject("Outlook.Application")
If Err.Number0 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 --

Ken Slovak - [MVP - Outlook] May 13th 08 02:11 PM

Late binding code
 
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"Stefan Hoffmann" wrote in message
...
hi Norman,

Norman Yuan wrote:
On Error Resume Next
Set OutlookApp = GetObject(,"Outlook.Application")
If Err.Number0 Then
Err.Clear
Set OutlookApp=CreateObject("Outlook.Application")
If Err.Number0 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 --



Norman Yuan May 13th 08 03:12 PM

Late binding code
 
You are right, I missed olMailItem.

"Douglas J. Steele" wrote in message
...
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)


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Norman Yuan" wrote in message
...
Dim OutlookApp As Object
Dim EM As Object

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

OutlookApp.GetNamespace("MAPI").Logon

Set EM = OutlookApp.CreateItem(olMailItem)

With EM
.To = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

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


"John" wrote in message
...
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 = "
.HTMLBody = "Something here..."
.Attachments.Add a(I)
.Display (False)
End With

Set EM = Nothing







All times are GMT +1. The time now is 03:37 PM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com