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. - Private Sub Approve_Click()
-
On Error GoTo Err_Approve_Click
-
DoCmd.GoToRecord , , acNewRec
-
-
DoCmd.OpenQuery "Qry_Final Capital Budget Approval"
-
-
DoCmd.SelectObject acForm, "Capital Request Finance", False
-
-
DoCmd.OutputTo acOutputForm, "Capital Request Finance", acFormatHTML, "Attachment Path", False, "", 0, acExportQualityPrint
-
-
-
-
Dim objOutlook As Outlook.Application
-
Dim objOutlookMsg As Outlook.MailItem
-
Dim objOutlookRecip As Outlook.Recipient
-
Dim objOutlookAttach As Outlook.Attachment
-
-
-
' Create the Outlook session.
-
-
Set objOutlook = CreateObject("Outlook.Application")
-
-
-
' Create the message.
-
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
-
-
With objOutlookMsg
-
' Add the To recipient(s) to the message.
-
Set objOutlookRecip = .Recipients.Add("recipient email address")
-
objOutlookRecip.Type = olTo
-
-
' Add the CC recipient(s) to the message.
-
'Set objOutlookRecip = .Recipients.Add("")
-
'objOutlookRecip.Type = olCC
-
-
' Add the BCC recipient(s) to the message.
-
'Set objOutlookRecip = .Recipients.Add("")
-
'objOutlookRecip.Type = olBCC
-
-
' Set the Subject, Body, and Importance of the message.
-
.Subject = "Capital Project Request"
-
.Body = "Please Review the following Capital Project Request." & vbCrLf & vbCrLf
-
.Importance = olImportanceHigh 'High importance
-
-
' Add attachments to the message.
-
If Not IsMissing(AttachmentPath) Then
-
Set objOutlookAttach = .Attachments.Add("Attachment Path")
-
End If
-
-
' Resolve each Recipient's name.
-
For Each objOutlookRecip In .Recipients
-
objOutlookRecip.Resolve
-
Next
-
-
' Should we display the message before sending?
-
'If DisplayMsg Then
-
' .Display
-
'Else
-
.Save
-
.Send
-
'End If
-
End With
-
Set objOutlook = Nothing
-
-
-
Err_Approve_Click:
-
'MsgBox "Error No.:" & Err.Number
-
'Resume Exit_Approve_Click
-
-
Response = acDataErrContinue
-
-
If Err.Number = 0 Then
-
-
MsgBox "Data has been successfully saved. Please click the Exit button to close application"
-
Else
-
MsgBox "Data Saved Successfully"
-
-
-
-
Resume Exit_Approve_Click
-
End If
-
Exit_Approve_Click:
-
Exit Sub
-
End Sub
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).
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. - 'Declaration for the API Function
-
Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" _
-
(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. - Dim retVal As Variant
-
-
'Adjust if necessary
-
Const conPATH_TO_OUTLOOK As String = "C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE"
-
-
-
If apiFindWindow(CStr("rctrl_renwnd32"), 0&) = 0 Then
-
'Outlook is not running, let's open it
-
retVal = Shell(conPATH_TO_OUTLOOK, vbMaximizedFocus)
-
Else
-
'Outlook is running, I'll leave the rest up to you!
-
End If
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.
@dlblack
How about executing the code in an AutoExec Macro or in your Start Up Form's Open() or Load() Event?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |