Using Late Binding to control Outlook (1 Viewer)

z0001130

New member
Local time
Today, 05:49
Joined
May 16, 2011
Messages
8
Hi,

I have managed to write some code to create a text file and email it as an attachment using the Reference method (early binding), but I fear I will run into problems when I put the DB on a machione that has a earlier version of Outlook.
I have searched and found several mentions of 'Late Binding' being the way I need to go but I cannot get it to work.
I would appreciate some help with any or all the follwoing possible solutions (and any others that you might think of)
1. To set the refernence in the code, and if there is an error, just run 'some other code' to create the text fil only and display a msgbox informing the user
2. Use late binding to send an email
3. Can code be used to send the email using smtp direct from the system.

Current code is :
Public Function SendEMail()
Dim db As DAO.Database
Dim MailList As DAO.Recordset
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
Subjectline$ = "On Movement File"
Set MyOutlook = New Outlook.Application
Set db = CurrentDb()
Set MailList = db.OpenRecordset("MyEmailAddresses")
Do Until MailList.EOF
Set MyMail = MyOutlook.CreateItem(olMailItem)

MyMail.To = MailList("email")
MyMail.Subject = Subjectline$
MyMail.Attachments.Add "c:\movement\bcms.txt", olByValue, 1
MyMail.Send
MailList.MoveNext
Loop
Set MyMail = Nothing
MyOutlook.Quit
Set MyOutlook = Nothing
MailList.Close
Set MailList = Nothing
db.Close
Set db = Nothing
End Function

Thanks to anyone who takes a loko at this and spends even a few minutes pondering it.
Regards,
John
 

z0001130

New member
Local time
Today, 05:49
Joined
May 16, 2011
Messages
8
Thanks, I will try that when I am home.

Do you know what I would have to check via code to skip the 'email' part and just create the file if Outlook was not present on the machine.

Thanks
John
 

z0001130

New member
Local time
Today, 05:49
Joined
May 16, 2011
Messages
8
Thanks for that one, that was the starting point I needed, lots more has come form your that small seed you planted.

One more small question, I am now able to send the email with the file attached to my list of emails in my table, but when I clean up after myself and tell outlook to quit, it trys to do so immediatly, while there are still unsent messages in the outbox. Do you the command to close outlook after it has finished sending?
 

JANR

Registered User.
Local time
Today, 14:49
Joined
Jan 21, 2009
Messages
1,623
Do you the command to close outlook after it has finished sending

I don't know other than instead of quiting Outlook you can hand over control of Outlook to the user and let them quit when they want.

.... Other code
MyOutlook.Display
... Other code

JR
 

darbid

Registered User.
Local time
Today, 14:49
Joined
Jun 26, 2008
Messages
1,428
Thanks for that one, that was the starting point I needed, lots more has come form your that small seed you planted.

One more small question, I am now able to send the email with the file attached to my list of emails in my table, but when I clean up after myself and tell outlook to quit, it trys to do so immediatly, while there are still unsent messages in the outbox. Do you the command to close outlook after it has finished sending?

Searching for comments to that question is optional as it is about 2 threads away from yours currently. http://www.access-programmers.co.uk/forums/showthread.php?t=209914
 

010397

New member
Local time
Today, 05:49
Joined
Jun 21, 2012
Messages
1
I am using the following to keep Outlook open until the outbox is empty.

Sub Close_Outlook()
'blueclaw-db(dot)com/read_email_access_outlook.htm
'for the PauseApp to work you must put this string at the top in the options compare section of the module.... Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Dim oOutApp As Object 'object for the outlook application
Dim IsItSent As Integer 'holds count of items in outbox of outlook
Dim objNameSpace As NameSpace Dim oMail As Object
Const cMailItem As Long = 0
'organizes an object defined in an assembly. Prevents name collision by qualifying the object by the full name spec.
Dim objFolder As MAPIFolder 'Represents a Microsoft Outlook folder. A MAPIFolder object can contain other MAPIFolder objects, as well as Outlook items.
'You can navigate nested folders by using a combination of Folders (index),
'which returns a folder within a name space or another folder, and the Parent property, which returns the containing object.
'There is a set of folders within an Outlook data store that support the default functionality of Outlook.
'Use GetDefaultFolder (index), where index is one of the OlDefaultFolders constants to return one of the default Outlook folders in the Outlook NameSpace object.
'The OlDefaultFolders constants are olFolderCalendar, olFolderContacts, olFolderDeletedItems, olFolderDrafts, olFolderInbox, olFolderJournal, olFolderNotes, olFolderOutbox, olFolderSentMail, olFolderTasks, olPublicFoldersAllPublicFolders, and olFolderJunk.
On Error GoTo Err_Handler
Set oOutApp = GetOutlookObject() 'binds the object to the latest version of outlook running
Set objNameSpace = oOutApp.GetNamespace("MAPI") 'binds the object to the outlook mapi
Set objFolder = objNameSpace.GetDefaultFolder(olFolderOutbox) 'specs which folder (outbox)
Set objRec = objNameSpace.CurrentUser
Set oMail = oOutApp.CreateItem(cMailItem)
IsItSent = objFolder.Items.Count 'update the count to determine if we need to loop
Do While IsItSent > 0
IsItSent = objFolder.Items.Count 'update the count inside the loop
PauseApp 10 'pause 10 seconds. The nice thing about the pauseapp function is that it has a low demand on cpu cycles
Loop
Set oOutApp = Nothing
Set oMail = Nothing
Set objNameSpace = Nothing
Set objFolder = Nothing
Set objRec = Nothing
Exit_Here:
Exit Sub
Err_Handler:
sMsg = Err.Description
If sType = "Contact" Then sMsg = sMsg & " data=" & sBody
MsgBox sMsg, vbExclamation, "Error"
Resume Exit_Here
End Sub

Public Function GetOutlookObject() As Object
'this procedure attempts to set the object to existing process of outlook.application
'and if the outlook process is not running it attempts to create it.
Dim oOutApp As Object
Dim sMsg As String
' We turn Error Handling OFF so we can attempt a call and test for errors.
On Error Resume Next
' If Outlook is already open, then use GetObject to set a reference to it.
' If you know version, then comment out the unneeded calls below.
Set oOutApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then ' Outlook 97 version
Err.Clear
Set oOutApp = GetObject(, "Outlook.Application.9")
End If
If Err.Number > 0 Then ' Outlook XP version
Err.Clear
Set oOutApp = GetObject(, "Outlook.Application.10")
End If
If Err.Number > 0 Then ' Outlook 2003 version
Err.Clear
Set oOutApp = GetObject(, "Outlook.Application.11")
End If
If Err.Number > 0 Then ' Outlook 2007 version
Err.Clear
Set oOutApp = GetObject(, "Outlook.Application.12")
End If
If Err.Number > 0 Then ' Outlook 2010 version
Err.Clear
Set oOutApp = GetObject(, "Outlook.Application.14")
End If
If Err.Number Then
Err.Clear
' If code failed to "Get" an instance of Outlook, then it isn't currently
' open and we must use CreateObject to open and set a reference.
Set oOutApp = CreateObject("Outlook.Application")
' If another error has occurred, then Outlook couldn't be opened.
' Inform user and abort.
If Err.Number > 0 Then
sMsg = "Could not open Outlook. " & vbCrLf & vbCrLf & _
"Either Outlook is not installed correctly, " & vbCrLf & _
"or there is a problem with the installation. " & vbCrLf & vbCrLf & _
"Try opening Outlook before running this utility. "
MsgBox sMsg, vbCritical, "Outlook Failed to Open"
Set oOutApp = Nothing
Exit Function
End If
End If

Set GetOutlookObject = oOutApp

End Function
 

Users who are viewing this thread

Top Bottom