Late Binding

raystownlaura

Registered User.
Local time
Today, 04:57
Joined
Apr 16, 2010
Messages
22
I'm using late binding vba code to send an automated email. The reason is that 2 of us use Access and Outlook 2007 and the other 2 use Access/Outlook 2003. Therefore when the database is opened on a 2003 machine, the reference to Outlook 12.0 Object Library gets broken.

We are a non-profit organization and have to work with what we have. We don't have the budget to upgrade all of us to Office 2007 so late binding is my only option.

I have never worked with late binding, so I would appreciate any help I can get to fix this code. It is erroring out telling me "Method or data member not found" when it gets to the following lines of code:

Code:
Set MAPISession = Outlook.NameSpace
Set MAPIFolder = Outlook.MAPIFolder
Set MAPIMailItem = Outlook.mailItem
Set oRecipient = Outlook.Recipient

The complete function is:


Code:
Public Sub RequestInvoice(ByVal EmailBody As String)
On Error GoTo ErrHandle
Const oMailItem As Long = 0

Dim oOutlook As Object
Dim emailItem As Object
Dim MAPISession As NameSpace
Dim MAPIFolder As MAPIFolder
Dim MAPIMailItem As Object
Dim oRecipient As Object

Set oOutlook = CreateObject("Outlook.Application")
Set emailItem = oOutlook.CreateItem(olMailItem)

[B]'CODE HANGS UP HERE WITH "METHOD OR DATA MEMBER NOT FOUND" ERROR[/B]
Set MAPISession = Outlook.NameSpace
Set MAPIFolder = Outlook.MAPIFolder
Set MAPIMailItem = Outlook.mailItem
Set oRecipient = Outlook.Recipient

'Get the MAPI NameSpace object
Set MAPISession = Application.Session

If Not MAPISession Is Nothing Then

  'Logon to the MAPI session
  MAPISession.Logon , , True, False

  'Create a pointer to the Outbox folder
  Set MAPIFolder = MAPISession.GetDefaultFolder(olFolderOutbox)
    If Not MAPIFolder Is Nothing Then

        'Create a new mail item in the "Outbox" folder
        Set MAPIMailItem = MAPIFolder.Items.Add(olMailItem)
            If Not MAPIMailItem Is Nothing Then
          
                With MAPIMailItem
                    Set oRecipient = .Recipients.Add("vsmith@raystown.org")
                    oRecipient.Type = olTo
                    Set oRecipient = Nothing
                    
                    Set oRecipient = .Recipients.Add("estoddard@raystown.org")
                    oRecipient.Type = olCC
                    Set oRecipient = Nothing
                    .Subject = "Invoice Request"
                    .Body = EmailBody
                    .Save
                    .Send
                End With
            End If
    End If
End If

Set MAPIMailItem = Nothing
Set MAPIFolder = Nothing
MAPISession.Logoff
Set MAPISession = Nothing
Set oOutlook = Nothing

Exit_ErrHandle:
    Exit Sub

ErrHandle:
    MsgBox "An error occurred in 'RequestInvoice' function." & vbCr & vbCr _
            & "Please provide the following information " & vbCrLf & "to the database administrator: " & vbCr & vbCr & "Error # " & Err.Number & ": " & Err.Description, vbCritical, "Error: " & Err.Number
    Resume Exit_ErrHandle
End Sub

Please remember, this is my only option since we are on a non-profit budget - we cannot upgrade two of our folks to Office 2007 so I must use late binding code.

Any help at all is greatly appreciated.

THANKS!!!
 
These should help, from one of my apps:

Set objOLApp = CreateObject("Outlook.application")
Set outNameSpace = objOLApp.GetNamespace("MAPI")
Set outFolder = outNameSpace.GetDefaultFolder(6) 'Inbox

You seem to think people won't think you should use late binding, but many of us are in similar situations. Late binding is pretty commonly used by developers.
 
Therefore when the database is opened on a 2003 machine,

I hope that doesn't mean that everyone is opening the same database file.
 
Thanks pbaldy.

I say that because a lot of the things I have read about this discourage late binding. But what is one to do when it's the only option :)
 
As long as it is a split database with the frontend as a copy on each user's machine then you shouldn't have a problem with references AS LONG AS you are doing development in the LOWER version of Access. But the statement you made in the first post made me wonder if each user was opening the same database file on a server somewhere or something and that could cause a real nightmare especially with different versions in place.
 
Thanks pbaldy.

I say that because a lot of the things I have read about this discourage late binding. But what is one to do when it's the only option :)

That is strange because I've never ever heard of someone discouraging late binding. It has always been ENCOURAGING late binding in order to keep from having reference problems. I usually use EARLY binding during DEVELOPMENT when I want to use Intellisense and then I switch it over to LATE BINDING for PRODUCTION.
 

Users who are viewing this thread

Back
Top Bottom