473,467 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to send email with attachment & access Outlook address book

beacon
579 Contributor
Hi everybody,

I'm sure that everyone cringes when they see "email" in the subject for posts in the forum, but I'm hoping that what I'm trying to do is different enough to present a challenge instead of an aneurysm. =)

I'm creating a form with a list box of values that, when selected, will create a filter for a report. On this same form, I want to add a command button that will allow the user to send an email with the report, based on the filter criteria, as an attachment. To make things a little bit more interesting, I would like to display the Outlook address book to the user to select the email addresses to send the report (I can't rely on the users to accurately enter email addresses and I don't want to import the contacts because my database is already projected to be extremely large).

I did some homework and found a way to display the Outlook address book using CDO, but once it's displayed I don't know how to capture the email addresses from the alias and then create the email, not to mention have the email automatically send without requiring the user to click 'Send' in Outlook.

Here's the code I "borrowed" from another site:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command43_Click()
  2.  
  3.     On Error GoTo err_Session_AddressBook
  4.  
  5.     Dim objOL As Outlook.Application
  6.     Dim oOutlook As Object
  7.     Dim oMail As Object
  8.     Dim objSession As MAPI.Session
  9.     Dim colCDORecips As Object
  10.     Dim strEmail As String
  11.  
  12.     On Error Resume Next
  13.  
  14.     Set objOL = GetObject(, "Outlook.Application")
  15.  
  16.     If Err.Number <> 0 Then
  17.         Set oOutlook = CreateObject("Outlook.Application")
  18.         Set oMail = oOutlook.CreateItem(olMailItem)
  19.         oMail.Display
  20.     End If
  21.  
  22.     Set objSession = New MAPI.Session
  23.     objSession.Logon "", "", False, False
  24.  
  25.     Set colCDORecips = objSession.AddressBook(, "Pick Names", , , 2, "To: ", "Cc: ")
  26.  
  27.     objSession.Logoff
  28.  
  29.     Set colCDORecips = Nothing
  30.     Set objSession = Nothing
  31.     Set objOL = Nothing
  32.     Set oOutlook = Nothing
  33.     Set oMail = Nothing
  34.  
  35. err_Session_AddressBook:
  36.     If (Err = 91) Then ' MAPI dlg-related function that sets an object
  37.         MsgBox "No recipients selected"
  38.     Else
  39.         'MsgBox "Unrecoverable Error:" & Err
  40.     End If
  41. End Sub
  42.  
Just for the record, I'm completely content using whatever method (including CDO) so long as the email will be sent automatically without the user having to click 'Send', the email will be sent to the users captured from the Outlook address book automation, and the report can be automatically attached without having to save the report to a network folder first.

Thanks,
beacon
Jul 28 '10 #1

✓ answered by beacon

Nevermind, I was able to resolve this issue.

For those that are interested, here's the code I used (I wasn't able to resolve the Outlook security, but you can't win them all):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command43_Click()
  2.  
  3.     Dim objOL As Outlook.Application
  4.     Dim oOutlook As Object
  5.     Dim oMail As Object
  6.     Dim objSession As MAPI.Session
  7.     Dim colCDORecips As Object
  8.  
  9.     On Error Resume Next
  10.  
  11.     Set objOL = GetObject(, "Outlook.Application")
  12.  
  13.     If Err.Number <> 0 Then
  14.         Set oOutlook = CreateObject("Outlook.Application")
  15.         Set oMail = oOutlook.CreateItem(olMailItem)
  16.     End If
  17.  
  18.     Set objSession = New MAPI.Session
  19.     objSession.Logon "", "", False, False
  20.  
  21.     Set colCDORecips = objSession.AddressBook(, "Pick Names", , True, 2, "To: ", "CC: ")
  22.  
  23.     If colCDORecips Is Nothing Then
  24.         Exit Sub
  25.     End If
  26.  
  27.     For Each col In colCDORecips
  28.         tmp = tmp & col & ";"
  29.     Next col
  30.  
  31.     strFileName = "C:\Temp\Contacts.snp"
  32.  
  33.     DoCmd.OutputTo acOutputReport, "Contacts", acFormatSNP, strFileName
  34.  
  35.     With oMail
  36.         .To = tmp
  37.         .Subject = "These two files"
  38.         .BodyFormat = olFormatHTML
  39.         .HTMLBody = "Please resolve the deficiencies in the attached report."
  40.         .Attachments.Add strFileName
  41.         .Send
  42.     End With
  43.  
  44.     objSession.Logoff
  45.  
  46.     'Cleanup
  47.     '---------------------------
  48.     Set colCDORecips = Nothing
  49.     Set objSession = Nothing
  50.     Set objOL = Nothing
  51.     Set oOutlook = Nothing
  52.     Set oMail = Nothing
  53.  
  54.     'Delete the file created by the SendObject method
  55.     '---------------------------------------------------
  56.     Set newFile = CreateObject("Scripting.FileSystemObject")
  57.     Set newFolder = newFile.GetFile(strFileName)
  58.     newFolder.Delete
  59.  
  60.     Exit Sub
  61.  
  62. err_Session_AddressBook:
  63.     If (Err = 91) Then ' MAPI dlg-related function that sets an object
  64.         MsgBox "No recipients selected"
  65.     Else
  66.         MsgBox "Unrecoverable Error:" & Err
  67.     End If
  68. End Sub
  69.  

1 3150
beacon
579 Contributor
Nevermind, I was able to resolve this issue.

For those that are interested, here's the code I used (I wasn't able to resolve the Outlook security, but you can't win them all):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command43_Click()
  2.  
  3.     Dim objOL As Outlook.Application
  4.     Dim oOutlook As Object
  5.     Dim oMail As Object
  6.     Dim objSession As MAPI.Session
  7.     Dim colCDORecips As Object
  8.  
  9.     On Error Resume Next
  10.  
  11.     Set objOL = GetObject(, "Outlook.Application")
  12.  
  13.     If Err.Number <> 0 Then
  14.         Set oOutlook = CreateObject("Outlook.Application")
  15.         Set oMail = oOutlook.CreateItem(olMailItem)
  16.     End If
  17.  
  18.     Set objSession = New MAPI.Session
  19.     objSession.Logon "", "", False, False
  20.  
  21.     Set colCDORecips = objSession.AddressBook(, "Pick Names", , True, 2, "To: ", "CC: ")
  22.  
  23.     If colCDORecips Is Nothing Then
  24.         Exit Sub
  25.     End If
  26.  
  27.     For Each col In colCDORecips
  28.         tmp = tmp & col & ";"
  29.     Next col
  30.  
  31.     strFileName = "C:\Temp\Contacts.snp"
  32.  
  33.     DoCmd.OutputTo acOutputReport, "Contacts", acFormatSNP, strFileName
  34.  
  35.     With oMail
  36.         .To = tmp
  37.         .Subject = "These two files"
  38.         .BodyFormat = olFormatHTML
  39.         .HTMLBody = "Please resolve the deficiencies in the attached report."
  40.         .Attachments.Add strFileName
  41.         .Send
  42.     End With
  43.  
  44.     objSession.Logoff
  45.  
  46.     'Cleanup
  47.     '---------------------------
  48.     Set colCDORecips = Nothing
  49.     Set objSession = Nothing
  50.     Set objOL = Nothing
  51.     Set oOutlook = Nothing
  52.     Set oMail = Nothing
  53.  
  54.     'Delete the file created by the SendObject method
  55.     '---------------------------------------------------
  56.     Set newFile = CreateObject("Scripting.FileSystemObject")
  57.     Set newFolder = newFile.GetFile(strFileName)
  58.     newFolder.Delete
  59.  
  60.     Exit Sub
  61.  
  62. err_Session_AddressBook:
  63.     If (Err = 91) Then ' MAPI dlg-related function that sets an object
  64.         MsgBox "No recipients selected"
  65.     Else
  66.         MsgBox "Unrecoverable Error:" & Err
  67.     End If
  68. End Sub
  69.  
Jul 30 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Phil Stanton | last post by:
Can anyone give me sone code to add addresses from a query to an Outlook Address book. The problem is that I want to add the new email addresses to a distribution list, not to the main email list ...
4
by: sfriedman | last post by:
I've heard that there was a linking utility that would permit me to link a table or query to my outlook address book. Can anyone tell me where to find this linking utility and how it works? The...
0
by: Adhiraj | last post by:
Hi, Does anyone hav any idea on how to access Microsoft Outlook Address Book thru' C#? Thanks in advance, Adhiraj.
1
by: Adhiraj | last post by:
Hi, Has anyone idea on how to access Microsoft Outlook Address Book thru' C# code? Thanks in advance, Adhiraj.
0
by: Bob Avallone | last post by:
MetaPro Systems Inc. Visual Studio Dot Net Tips & Tricks #3 – Direct Access to Your Outlook Address Book. Project Type: VS.NET Windows Application Code Behind: Visual Basic I have a project...
0
by: Allophylus | last post by:
Hi, I am developing a simple utility that will queries Active Directory and generate a folder in the users MS Outlook 2000 with the respective users' contacts and distribution list. Everything...
4
by: mattkorguk | last post by:
Good morning All, I have built a small database usage utility which basically shows who logged into the Db, when and what version was used. I'm bringing back the users log on Id such as...
4
by: Alexander Szigetvary | last post by:
Hi NG! Is there a way to open the Outlook address book dialog (as in Outlook) using C#? I am using Outlook 2003 (MS Outlook 11.0 Object Library). In Outlook 2007 it can be done with...
1
by: Lpitt56 | last post by:
I am running MS Access 2007 and I want to update an Outlook Address book from my Access Database. I started out by importing the Outlook Address Book as a linked table and it linked fine. I then...
10
by: Mike Miller | last post by:
Hi, Am wanting to send email with php and need to access the global outlook address book. Are there any examples/tutorials on how to do this? M
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.