473,388 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Opening Outlook from Access when Outlook is Closed

I am using the following code to send emails with attachments from Access using Outlook. The code works fine if Outlook is already open and emails are sent as expected via Outlook. When Outlook is closed, data is saved to the database but the email is not sent. Here is the the code. If anyone has suggestions on how I can get Outlook to open before trying to send the email I would appreciate it.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Approve_Click()
  2. On Error GoTo Err_Approve_Click
  3.     DoCmd.GoToRecord , , acNewRec
  4.  
  5.     DoCmd.OpenQuery "Qry_Final Capital Budget Approval"
  6.  
  7.     DoCmd.SelectObject acForm, "Capital Request Finance", False
  8.  
  9.     DoCmd.OutputTo acOutputForm, "Capital Request Finance", acFormatHTML, "Attachment Path", False, "", 0, acExportQualityPrint
  10.  
  11.  
  12.  
  13.           Dim objOutlook As Outlook.Application
  14.           Dim objOutlookMsg As Outlook.MailItem
  15.           Dim objOutlookRecip As Outlook.Recipient
  16.           Dim objOutlookAttach As Outlook.Attachment
  17.  
  18.  
  19.           ' Create the Outlook session.
  20.  
  21.           Set objOutlook = CreateObject("Outlook.Application")
  22.  
  23.  
  24.           ' Create the message.
  25.           Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  26.  
  27.           With objOutlookMsg
  28.               ' Add the To recipient(s) to the message.
  29.               Set objOutlookRecip = .Recipients.Add("recipient email address")
  30.               objOutlookRecip.Type = olTo
  31.  
  32.               ' Add the CC recipient(s) to the message.
  33.               'Set objOutlookRecip = .Recipients.Add("")
  34.               'objOutlookRecip.Type = olCC
  35.  
  36.              ' Add the BCC recipient(s) to the message.
  37.               'Set objOutlookRecip = .Recipients.Add("")
  38.               'objOutlookRecip.Type = olBCC
  39.  
  40.              ' Set the Subject, Body, and Importance of the message.
  41.              .Subject = "Capital Project Request"
  42.              .Body = "Please Review the following Capital Project Request." & vbCrLf & vbCrLf
  43.              .Importance = olImportanceHigh  'High importance
  44.  
  45.              ' Add attachments to the message.
  46.              If Not IsMissing(AttachmentPath) Then
  47.                  Set objOutlookAttach = .Attachments.Add("Attachment Path")
  48.              End If
  49.  
  50.              ' Resolve each Recipient's name.
  51.              For Each objOutlookRecip In .Recipients
  52.                  objOutlookRecip.Resolve
  53.              Next
  54.  
  55.              ' Should we display the message before sending?
  56.              'If DisplayMsg Then
  57.               '   .Display
  58.              'Else
  59.                 .Save
  60.                 .Send
  61.              'End If
  62.           End With
  63.           Set objOutlook = Nothing
  64.  
  65.  
  66. Err_Approve_Click:
  67.      'MsgBox "Error No.:" & Err.Number
  68.     'Resume Exit_Approve_Click
  69.  
  70.     Response = acDataErrContinue
  71.  
  72.     If Err.Number = 0 Then
  73.  
  74.       MsgBox "Data has been successfully saved. Please click the Exit button to close application"
  75.     Else
  76.         MsgBox "Data Saved Successfully"
  77.  
  78.  
  79.  
  80.     Resume Exit_Approve_Click
  81.     End If
  82. Exit_Approve_Click:
  83.     Exit Sub
  84. End Sub
Feb 19 '09 #1
6 10593
NeoPa
32,556 Expert Mod 16PB
I've not used automation in Outlook, but check out some details on how to automate and what to use in which circumstances (Application Automation).
Feb 20 '09 #2
ADezii
8,834 Expert 8TB
I'm rather in a rush right now, but I'll post the code for you, and should you have any questions whatsoever, some of the other Experts/Moderators/Members, will be happy to assist you. I'll check back on this Thread later if you need specifics on the code.
Expand|Select|Wrap|Line Numbers
  1. 'Declaration for the API Function
  2. Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" _
  3.  (ByVal strClassName As String, ByVal lpWindowName As Any) As Long
See if Application is running by attempting to retrieve its Handle by passing its Class Name to apiFindWindow(). If it isn't running, launch it, if it is, do whatever you 'wish.
Expand|Select|Wrap|Line Numbers
  1. Dim retVal As Variant
  2.  
  3. 'Adjust if necessary
  4. Const conPATH_TO_OUTLOOK As String = "C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE"
  5.  
  6.  
  7. If apiFindWindow(CStr("rctrl_renwnd32"), 0&) = 0 Then
  8.   'Outlook is not running, let's open it
  9.   retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus)
  10. Else
  11.   'Outlook is running, I'll leave the rest up to you!
  12. End If
Feb 20 '09 #3
I think the code referenced by ADezii will work but being a newbie at VBA, I'm not sure how to integrate the new code into my existing code. I have my existing code running behind my forms and not in a module. I have been researching as to whether I need to put this in a module and the more research I do the more confused I get. Any suggestions would be helpful.
Feb 20 '09 #4
NeoPa
32,556 Expert Mod 16PB
Did you check out the Application Automation link?
Feb 20 '09 #5
ADezii
8,834 Expert 8TB
@dlblack
How about executing the code in an AutoExec Macro or in your Start Up Form's Open() or Load() Event?
Feb 21 '09 #6
ADezii
8,834 Expert 8TB
I've finally sat back and took a closer look at things. If you are using Automation code to E-Mail and send Attachments via Outlook, when you create a New Instance of the Application, you are in effect 'Opening' it, and you can retrieve a Handle to it. I cannot actually see why your code is not working, and you need to 'Open' Outlook externally.
Feb 21 '09 #7

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

Similar topics

3
by: Jordan | last post by:
I created and access application that sends e-mails automaticaly to our clients, over 1.500 e-mails daily. I am using the MS Outlook components to create the e-mail with HTML code. All works...
3
by: deko | last post by:
When adding Outlook Appointment Items from Access, should I use: Set ol = New Outlook.Application --or-- Set ol = CreateObject("Outlook.Application") Outlook seems to crash either way. The...
1
by: Francisc Molnar | last post by:
Hi. I'm trying to access the appointments from MS Outlook 2003 using the interop assembly. Here is the code using .Net Framework 2.0 beta: Microsoft.Office.Interop.Outlook.Application...
10
by: John | last post by:
Hi When I open a new outlook email from vb.net, sometimes outlook is very slow to appear or occasionally outlook freezes completely. I am targeting mixed office2000/xp environments so I am...
2
by: Snig | last post by:
Hi I'm into creating a web application in C# (.NET Framework 1.1) which would list and manage the outlook tasks. If I try to do this in a Windows Application, it works fine. But there are...
2
sivisr
by: sivisr | last post by:
Hi, i am using the following code to open outlook Express to send mail ,but i get " System.UnauthorizedAccessException: Access is denied. at WebApplication7.WebForm9.Button1_Click(Object...
2
by: =?Utf-8?B?RU1jQ2FydGh5?= | last post by:
My organization currently has interfaces in place to generate MS Outlook emails from our business applications. I am trying to determine if it is possible and how difficult it might be to...
2
by: LeoK | last post by:
After finally completing my database I was ready to transfer it to our client, but when the access database was opened, everything works great, except for the SEND_EMAIL part, whenever any code that...
0
by: bbrewder | last post by:
I am struggling with some MSAccess automation issues. Basically, we have a .Net application that uses MSAccess for reporting (legacy code). We are able to launch MSAccess fine and even work with...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.