My Assistant
![]() ![]() |
|
|
Oct 20 2010, 06:29 AM
Post
#1
|
|
|
New Member Posts: 2 |
I am currently converting an Access 2000 application to Access 2010. I need to be able to send emails automatically from the Access application.
The code below works fine in Access/Outlook 2000. It also works fine in Access/Office 2010 but ONLY if Outlook is open in the background. If Outlook is not open then I get a "Run-time error '287'" at the ".Recipients.Add" line. In Access/Outlook 2000 Outlook does not need to be open for this to work (but it does always show the message box with the "A program is trying to access e-mail addresses you have stored in Outlook......." message) What I would like to be able to do is send an email from Access 2010 without having to open Outlook manually. Is this possible? Many thanks, Tony Sub SendMail() Dim objApp As Outlook.Application Dim nspNameSpace As Outlook.NameSpace Dim objNewMail As Outlook.MailItem Dim objRecipient As Outlook.Recipient Dim blnResolveSuccess As Boolean Set objApp = New Outlook.Application Set nspNameSpace = objApp.GetNamespace("MAPI") Set objNewMail = objApp.CreateItem(olMailItem) With objNewMail .Recipients.Add "me@example.com" blnResolveSuccess = .Recipients.ResolveAll .Subject = "Test" .Body = "This is a test email" If blnResolveSuccess Then .Save .Send Else MsgBox "Unable to resolve all recipients" .Display End If End With SendMail_Exit: Set objRecipient = Nothing Set objNewMail = Nothing Set nspNameSpace = Nothing Set objApp = Nothing Exit Sub SendMailMail_Error: MsgBox Err.Description Resume SendMail_Exit End Sub |
|
|
|
Oct 20 2010, 04:26 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 638 From: Sydney, Australia |
If you use SMTP email instead, it'll go straight to your email server, bypassing Outlook (and the annoying warning). Add the following to a standard module, and modify the bits noted. If you can't work out how to use it, come back and ask.
CODE Public Sub SendSMTPEmail(strSubject As String, _
strBodyText As String, _ strRecipient As String, _ Optional varCC As Variant, _ Optional varBCC As Variant, _ Optional varFrom As Variant, _ Optional colAttachmentPath As VBA.Collection) Dim objMessage As Object Dim objConfig As Object Dim objFields As Object Dim intIndex As Integer Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). Const cdoAnonymous = 0 'Do not authenticate Const cdoBasic = 1 'basic (clear-text) authentication Const cdoNTLM = 2 'NTLM 'Delivery Status Notifications Const cdoDSNDefault = 0 'None Const cdoDSNNever = 1 'None Const cdoDSNFailure = 2 'Failure Const cdoDSNSuccess = 4 'Success Const cdoDSNDelay = 8 'Delay Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay Set objMessage = CreateObject("CDO.Message") Set objConfig = CreateObject("CDO.Configuration") '==This section provides the configuration information for the remote SMTP server. Set objFields = objConfig.Fields With objFields .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort 'Name or IP of Remote SMTP Server .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.server-name.com" '*** ADD SMTP SERVER NAME HERE 'Type of authentication, NONE, Basic (Base64 encoded), NTLM .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic 'Your UserID on the SMTP server .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "account-name" '*** ADD ACCOUNT NAME HERE 'Your password on the SMTP server .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "account-password" '*** ADD ACCOUNT PASSWORD HERE 'Server port (typically 25) .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'Use SSL for the connection (False or True) .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server) .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 .Update End With With objMessage Set .configuration = objConfig .Subject = strSubject .From = IIf(IsMissing(varFrom), "default@email-address.com", varFrom) '*** ADD A DEFAULT EMAIL ADDRESS HERE .To = strRecipient .TextBody = strBodyText If Not (colAttachmentPath Is Nothing) Then For intIndex = 1 To colAttachmentPath.Count .AddAttachment colAttachmentPath(intIndex) Next intIndex End If .Send End With Set objMessage = Nothing Set objConfig = Nothing Set objFields = Nothing End Sub |
|
|
|
Oct 21 2010, 09:16 AM
Post
#3
|
|
|
New Member Posts: 2 |
Graham,
Thank you very much for the code - it has solved my problem. Regards, Tony |
|
|
|
Dec 29 2011, 01:24 PM
Post
#4
|
|
|
New Member Posts: 1 |
I am trying to send out emails without the warning coming up and this looks like a good solution, could it be modified to work with this also?
Private Sub Form_Load() If (Forms!frmRetailProductionLogOpenSplit!RegBDays > 25 And Forms!frmRetailProductionLogOpenSplit!RegBComplianceNotified = 0) Then DoCmd.SendObject acReport, "rptRetailProductionLogRegB", "PDFFormat(*.pdf)", "test1@bank.com", "test2@bank.com", "test3@bank.com", "Reg B", """Check the Reg B status on the attached Report"".", False, "" End If Any help would be greatly appreciated. This post has been edited by Paul_Harth: Dec 29 2011, 01:25 PM |
|
|
|
Dec 26 2012, 07:38 AM
Post
#5
|
|
|
New Member Posts: 1 |
I tried this when using Access 2010 VBA. It works on one computer (it hs the Microsoft CDO for Windows 2000 Library isntalled), but none of the others. They get a 429 error at the spot where it tries to do any CDO command.
I tried loading the CDO 1.2.1, but it won't load on the computers that have Outlook 2010 installed. Any ideas on how I can make this work on computers with Office 2010 installed? |
|
|
|
Apr 24 2013, 08:50 AM
Post
#6
|
|
|
New Member Posts: 1 |
I found a workaround after haveing Run Time Error 287 , due to Outlook not being on the same server as Access 2010. The first With OlkMsg creates a dummy email first then the second With OlkMsg, shows the Allow Access pop up box, then creates another email with all the correct fields and overwrites the first email. Not the best but quick and simple.
Set Olk = CreateObject("Outlook.Application") Set OlkMsg = Olk.CreateItem(olMailItem) With OlkMsg .To = "Delete Me" .Subject = "Delete Me" .HTMLBody = "Delete Me" .Display End With With OlkMsg Set OlkRecip = .Recipients.Add(Address) OlkRecip.Type = olTo .Subject = Title .HTMLBody = Message If (Len(Dir(File1, vbDirectory)) > 0) And (File1 <> "") Then .Attachments.Add File1 End If .Display End With |
|
|
|
May 9 2013, 04:43 PM
Post
#7
|
|
|
New Member Posts: 9 |
Another solution, one that I use that is simple and easy:
Function CDO_Named_As_Preferred() ' This Sub is the CDO Code to email items without the Outlook security prompt Dim objMessage As Object Set objMessage = CreateObject("CDO.Message") objMessage.Subject = "Subject message" objMessage.From = "Email address" objMessage.To = "Email address; Email address" objMessage.CC = "Email address" objMessage.TextBody = "Text body" objMessage.AddAttachment "Attachment path" '==This section provides the configuration information for the remote SMTP server. '==Normally you will only change the server name or IP. objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/CDO/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/CDO/configuration/smtpserver") = "server name" 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/CDO/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update '==End remote SMTP server configuration section== objMessage.Send End Function |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 19th May 2013 - 01:59 PM |