Send Email outlook using Excel VBA - excel

I am trying to send an email using Excel with this code :
Sub SEND_EMAIL()
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
' ADRESSE
olMail.To = "selojo7915#rippb.com"
' Subject
olMail.subject = "Line"
' BODY
olMail.body = "this"
olMail.Send
When starting this macro i have the following error
The error is in the last line (olMail.Send)
And every time i am trying to send an email outlook is starting to update and after a moment it's showing error
Exception de HRESULT : 0x80004004 (E_ABORT)

You see a security issue (earlier prompts) because Outlook is configured on the client computer in one of the following ways:
Uses the default Outlook security settings (that is, no Group Policy set up)
Uses security settings defined by Group Policy but does not have programmatic access policy applied
Uses security settings defined by Group Policy which is set to warn when the antivirus software is inactive or out of date
You can create a group policy to prevent security prompts from displaying if any up-to-date antivirus software is installed on the system or just turn these warning off (which is not really recommended).
Read more about that in the Security Behavior of the Outlook Object Model article.
You may consider using the following workarounds:
Install any antivirus software with latest updates.
Use a low-level API - Extended MAPI which doesn't trigger security issues. You may also consider Redemption which is a convenient wrapper around Extended MAPI.
Create a COM add-in which has access to the safe Application instance.
Use group policy settings to turn such security prompts/issues when dealing with Outlook.
Also you may consider using third-party components that allow turning off and on dynamically security prompts and issues in Outlook. See Outlook Security Manager for more information.

Related

Excel VBA email loop - server connectivity error handling

I have an automated Excel VBA loop which sends out a couple hundred e-mails daily over Outlook with a couple second intervals. Running this in Windows 10 + Office 365 environment.
While it runs fine in general, it crashes at times with error message We can't complete this because we can't contact the server right now. Please try again later
Internet connectivity is generally OK, but can't rule out a brief disturbance. It happens once every 1-2 weeks during the loop, but is an annoyance as it stops the automation. Email syntax is what I see as quite typical practice in Excel VBA -> Outlook email (stripped down version):
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
.To = Range("Array_emp_email")
.SentOnBehalfOfName = "mailbox#domain.com"
.Subject = "mySubject"
.HTMLBody = "mailBody" & Signature
.Attachments.Add docPath & ".pdf"
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
I'm stopped at Set OutMail before a new Outlook mail item should be opened.
When I press Debug and then Run/Continue, it always runs on, without exception, so making it programmatically try this line again, might be a possible solution? Do you guys have any suggestion on what would be a good practice to handle it?
Using plain On Error Resume Next and leave one e-mail unsent is not an option. Leaving e-mail unsent, but logging an error through On Error GoTo ErrHandler and closing the sub is something I could do myself, but that's less than ideal also, since it's an hassle to compile the same email again.
Ideally it would be "take a minute and try again" approach, but are there any good practices or code examples how to do that here?
If you are using online (as opposed to cached) in Outlook, it is guaranteed you will run into issues are this - network errors are unavoidable and must be expected.
You need to either turn the cached mode on (Outlook will deal with any network errors when it syncs your changes) or you need handle the error and retry later (seconds? minutes?).
Outlook provides two basic connectivity modes when you are connected to Exchange Server: Cached Exchange Mode and Online Mode.
Cached Exchange Mode gives users a seamless online and offline Outlook experience by caching the user's mailbox and the Offline Address Book (OAB) locally. With Cached Exchange Mode, which is the default setting for users, Outlook no longer depends on continuous network connectivity for access to user information. When a user is connected, Outlook continuously updates users' mailboxes so that the mailboxes are kept up to date. If a user disconnects from the network, for example, by moving to an area without Wi-Fi access, the user can continue to access the last available email data.
Online Mode works by using information directly from the server, and, as the name implies, it requires a connection. Mailbox data is only cached in memory and never written to disk. So, if you have connection problems with Exchange in the online mode, you may get such errors. I'd suggest enabling the cached mode to avoid any issues with a connection, see Turn on Cached Exchange Mode for more information.
Microsoft recommends always using Cached Exchange Mode with a Microsoft 365 account.

Run Outlook Macro when sending mail from Excel

I have a VBA code in Thisoutlooksession that runs when new mail is sent.
But when I send a mail using Excel biltin option to send the file attached, the code in the outlook don't run.
Would appreciate youre help.
Thanks in advance.
This is a situation where some Outlook functionality is not applied.
"You use Send As or other Send commands from another Microsoft Office application, such as Microsoft Word, Microsoft Excel, or Microsoft PowerPoint. This scenario typically occurs when you use a command to send the current file as an attachment." Messages that are created outside Outlook do not include the default Outlook email signature
"If you need all features to be available, you either need to create the email from Outlook itself or create the new email programmatically from within that other application as well." Outlook UserForms dont work when emailing from another application
"This is by design: messages created and sent by Simple MAPI or mailto url do not trigger OOM events. This is done on purpose - there were some problems in those scenarios, so events were completely disabled." ItemSend event is not firing

Excel Auto Send Emails via Outlook Randomly NOT Sending

I've inherited development of an Excel userform which contains a send button to return the user's data back to a central mailbox. The tool has just been rolled out to a larger community and some users are experiencing emails not being sent. These are random events as the users can sometimes send the forms and it is not form specific.
The tool uses the following code to create the email:
Set ObjOL = CreateObject("Outlook.Application")
Set ObjOutMail = ObjOL.CreateItem(0)
and is sent via:
ObjOutMail.send
We had tested this with a number of users and never had any problems, but we now have a number of users who have successfully sent 1 or more forms but 1 or more have also failed, and they also can't resend the successful forms any more either.
There are no error messages, the Outlook "this could be a virus, etc." confirmation is fired as usual (we have not attempted to circumvent this) but there is nothing in the 'Sent Items'. The users are also able to send conventionally generated emails so there is no mailbox limitations going on.
The Userform is modal and upon sending copies the three user fields to "Sheet1", saves itself and mails itself.
Really at a loss to understand what's going on.
Thanks for any advice.
EDIT: To add we are still using Office 2003 on Win XP.
Thanks for all the replies, using the .display property showed me what was up.
The email is addressed to what I thought was an explicit address, however there is an extra '(MULTIUSER)' post fix which I didn;t realise was actually part of the address, DOH!.
Last week and similar mailbox made up of the same address but with a different postfix was added to the system. This ment Outlook couldn't resolve the address and didn't send the emails.
Thanks again.

ASP.NET Check new mail on Lotus Notes/Domino server

Is there a way to check new mail on a Lotus Notes/Domino server in ASP.NET 3.5+, for multiple users. Users are logged in with there Windows Domain account into the ASPx application.
Given, that the Domino servers are also on Windows and they have SPNEGO activated for SSO, you have several options, depending on what you mean by "check new mail".
If you want to include the Inbox in your ASP.Net app eg. as an iframe, you can use iNotes portal mode to accomplish that.
If you are on Domino 8.5.3, you can use the REST services of the new extension library to access the Inbox contents via a few REST calls and Json. See the link for more infos.
If you want to access the "new mail" in the backend of your ASP.Net app, you are problably out of luck, as the information about "new/unread" mail is stored per user and you would need the ID-File/password or http password of the user to access that information.
But maybe you could provide more info, so we can give better advice.
Well, yes you can, but it's not a straight up webservice call. I'm not aware of any turn key configuration that would enable this. So, you will need to "roll your own" service. If so, then you'll be interested in "Unread marks" or "Unread email". This is a unique function to Lotus Notes and is not exposed as service in Java or .Net. But you can get around it by using the "GetAllUnreadEntries" method in the Lotus Notes object API.
Using this method, you can programmatically emulate it without too much complexity. Is it possible for you to try this:
Can you implement in an ASP.Net application a "last Checked" date/time value that is set when you traverse the inbox of Domino mail account ?
Now whilst looping through the inbox, get the created date of each document.
In the case of email in a Lotus Notes database, this is the date the email hit the account. So it should be a fairly reliable means of determining the arrival date of the email.
The created date property is under the NotesDocument object as "created". This should return a date/time value that you can use. Any document that is newer than the "last checked" value would therefore be new mail.
If you have a particularly large inbox to loop through, you can get the inbox object (which can be treated like a view), and also use "GetAllUnreadEntries" method on the NotesView object.
Links to example code are in the links above.

How can I determine a new Reply/Forward message's parent MailItem in an Outlook 2010 C#.NET Add-in

I am trying to develop an Add-in for Outlook 2010 that allows me to tag an email using an Outlook.MailItem's UserProperties collection. The fields are loaded from a database configuration and displayed in a task pane for population by the user when composing the email. When someone with the Add-in receives the email they can open it up and view/edit the tagged values in the same task pane.
I also want to populate a reply or forward message spawned from the tagged email with the current values. Unfortunately, I have found no way to determine what Outlook.MailItem the new email originated from. I tried using the newly created email passed to Reply event to find the parent, using the Parent property or GetConversation() method, both of which return null for the newly created item.
So, to summarize: is there a way to determine the spawning MailItem from which a new Reply or Forward MailItem is generated? If you need any clarification on this issue, let me know.
Thanks,
Andrew
I'm currently developing an add-in for Outlook 2007/2010 which makes use of one user-property. I've made the experience that a MailItem with the user property set inherits it when it is forwarded. Have you already looked for the user property/properties in the new e-mail? You can use Outlook Spy from Dmitry Streblechenko to explore them better.

Resources