Send email reminder when task occurs - vba

This code works with Outlook's Task system. I created a task that reoccurs every week on Wednesday. When the task occurs the code is suppose to send an email to "test#work.com" (not the real test email).
I have written the code in the Outlook VBA developer window.
Am I missing a step to get the code to run? The code logic is posted below.
Sub Application_Reminder(ByVal Item As Object)
Dim objPeriodicalMail As MailItem
If Item.Class = olTask Then
If InStr(LCase(Item.Subject), "Send PM Work Order Request Reminder Email") Then
Set objPeriodicalMail = Outlook.Application.CreateItem(olMailItem)
With objPeriodicalMail
.Subject = "REMINDER-Get Work Order Request Submitted"
.To = "Test#work.com"
.HTMLBody = "<HTML><BODY>Hello All Pm's, Please Submit your Work order request by 9:00A.M.</HTML></BODY>"
.Attachments.Add
.Importance = olImportanceHigh
.ReadReceiptRequested = True
.Send
End With
End If
End If
End Sub

To control Outlook objects from outside Outlook, you must establish a reference to the Outlook object library from the project in which you are writing code. To do this, use the References dialog box in the Visual Basic Editor in the primary application. You can then write code that returns a reference to the Outlook Application object. Through this reference, your code has access to all the objects, properties, methods, and constants defined in the Outlook type library.
Read more about that in the Automating Outlook from a Visual Basic Application article.
To set up events you need to define the source object withevents or select in the drop-down list on VBA editor and select the required event. See Using Outlook Visual Basic for Applications to Respond to Outlook Events.

Related

How to enable macros, to send email with Outlook 2016 when a reminder fires?

I want to send email every time a recurring reminder fires.
I am using VBA code from
https://www.slipstick.com/developer/send-email-outlook-reminders-fires/
The only change I made is to set the .BCC to my email address (not shown in this post).
I do not know how to tell whether the code executes, or whether it executes but does not do what I want. Is there a way to know whether it executes?
ADDITIONAL DESCRIPTION OF PROBLEM
I use alt-F11 to get to my VBA code. It does not show up when I try to access it as a macro.
My code used to run when a reminder fired.
I tried multiple ways to get my code to run.
I wanted to attach screen shots but I could not find out how.
I have my VBA code in both of these places:
Project 1 (VbaProject.OTM) - Microsoft Outlook Objects – ThisOutlookSession
Project 1 (VbaProject.OTM) – Modules – Module1
File > Options > Addins shows the “Microsoft VBA for Outlook Addin” in “Active Application Add-Ins”.
However, when I go to Developer tab > Com Add-Ins “Microsoft VBA for Outlook Addin” is not listed.
On that screen, I clicked Add and chose OUTLVBA.DLL from this folder: C:\Program Files (x86)\Microsoft Office\root\Office16\ADDINS To try to add it. I got this error message:
“ ’C:\Program Files (x86)\Microsoft Office\root\Office16\ADDINS\OUTLVBA.DLL’ is not a valid Office Add-in.”
“Microsoft VBA for Outlook Addin” is not listed in Developer tab > Disabled Items
I have gone to Developer tab > Macro Security. I tried the fourth option (Enable all macros), but it still did not work, so I set it back to option 2 (Notifications for digitally signed…”. That is what I had before when the code used to run.
When I go to Developer tab > Macros > Macros and create a small macro and then click Run, I get this message:
“The macros in this project are disabled. Please refer to the online help or documentation of the host application to determine how to enable macros.”
I have both clicked Help and googled but not found out how to enable macros.
Private Sub Application_Reminder(ByVal Item As Object)
Dim objMsg As MailItem
'IPM.TaskItem to watch for Task Reminders
If Item.MessageClass <> "IPM.Appointment" Then
Exit Sub
End If
If Item.Categories <> "Send Mail" Then
Exit Sub
End If
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
.To = Item.Location
.BCC = "xxx#centurylink.net"
.Subject = Item.Subject
.Body = Item.Body
.Send
End With
Set objMsg = Nothing
End Sub
Check if the message is in the Sent Items folder.
Add a line like MsgBox "Test" to the beginning of your Application_Reminder event handler.

Send mail when Outlook is not already open

My situation:
I'm trying to send e-mails while Outlook is not open. This code will work if Outlook is running. If Outlook is closed the code will create a non-visible process that you can see running in Task Manager.
The code encounters an error during the .Send. It returns runtime error 287.
From my experience that are certain VBA methods that will only work when the object is either visible or active.
My work around is to use .Display(False) before calling .Send.
After calling .Send it immediately terminates the Outlook process. (Please point me to the right direction why.) Then the e-mail gets stuck in the Outbox.
I have to apply another work around by calling another CreateObject("Outlook.Application") and either looping through the Outbox While Outbox.Items.Count > 0
or
Looping through SyncObjects and manually calling .Start to run Send/Receive on the Outbox.
My question:
Is there a way to directly use the .Send method instead of using work arounds while also not having to open Outlook.
My code:
Sub emailer()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "f#r.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
A workaround for the .send issue with Outlook closing can possibly be resolved by changing the settings for Send/Receive. Go to File -> Options -> Advanced -> find Send/Receive... and press it. Under "Setting for group "All Accounts" is a checkbox for Perform an automatic send/receive when exiting. Check this, and it may resolve the "stuck in the outbox issue you are having".
Here is a screenshot:
Give this a try, hopefully it helps.
Use Namespace.SendAndReceive. Keep in mind that Send/Receive is asynchronous and you need to wait to the sync to finish. You can use Namespace.SyncObjects collection, start a sync using SyncObject.Start, then wait for the SyncObject.SyncEnd event.
To prevent Outlook from closing, you need to keep a live reference to an Outlook Explorer or Inspector object. You can retrieve the Inbox folder using Namespace.GetDefaultFolder, then use MAPIFolder.GetExplorer method to get a pointer to an Explorer object.
UPDATE:
You might also want to make sure that you log to a MAPI profile before you create and send a message. After the CreateObject line, try to add the following
set NS = OutApp.GetNamespace("MAPI")
NS.Logon

Generating Email with Hyperlink from MS Access

I'm attempting to generate an email from MS Access when a particular procedure is run and certain conditions are met, the email will include a hyperlink. I've found the sendobject macro command does not allow for hyperlink, only static text. It seems that the solution is to code the portion of the entire process that generates and sends the email in VBA and call on that code in the appropriate segment of my if function within my macro.
I can't figure out the appropriate code to generate and send and email with a hyperlink to an individual however. It will be very simple, single recepient, unchanging title, and the body will read 'New providers require designation, please access the provider designation dashboard for provider designation' ideally the provider designation dashboard would be the hyperlink and point to a shared network space.
What commands do I need to accomplish this, I'm inexperienced in VBA and this is eating up a fair amount of time I don't have.
Thank you
There are some different approaches for sending e-mail with code. The code bellow uses an Outlook Application COM object to generate the message with a hyperlink - thus, it will only work if MS Outlook is installed in the user's machine.
Sub NewEmail(ByVal mylink As String, ByVal therecipient As String)
Dim Outlook As Object, Email As Object
Set Outlook = CreateObject("Outlook.Application")
Set Email = Outlook.CreateItem(0) 'olMailItem = 0
With Email
.Subject = "My Subject" 'message subject
.HTMLBody = "Greetings, please check this link: <a href='" & mylink & "'>Click me</a>." 'message body, in html. concatenate multiple strings if you need to
.To = therecipient 'recipient
'use this if you want to generate the message and show it to the user
.Display
'use this instead if you want the mail to be sent directly
'.Send
End With
Set Email = Nothing
Set Outlook = Nothing
End Sub
Put the code in a module. Then anywhere in your code you may call the procedure with something like:
NewEmail "www.mysite.com/targetpage.html", "persontomail#domain.com"
Notice that the routine above uses late binding. To use early binding (and get intellisese, but with some drawbacks), you would have to add a reference to Microsoft Outlook XX.X Object Library and dim the "Outlook" and "Email" objects as Outlook.Application and Outlook.MailItem, respectively.

Accessing "From:" from VBA in Outlook 2010

I have on account "me#domain.com" configured in Outlook 2010. I compose a message and open the drop-down on "From:". I select "Other email address..." and type in "bingo#bongo.com". I get a pop-up asking whether to send messages from "bingo#bongo.com" via the "me#domain.com" account. I "ok" that. When I now send a message to "someone#domain.com", the recipient sees
From: bingo#bongo.com
Curiously, when I go to inspect me#domain.com/Sent Items, all I see is
From: me#domain.com
I am very frustrated about this behaviour because I wish to move sent items depending on the sending account. Initially I looked into creating a Send Rule. Irritatingly, there is no option to action anything based on "From:". So I dive into VBA. I got most of the code to move the stuff, but when I look through the mail item object (in the Locals window), I cannot find any property that states "bingo#bongo.com".
Can anyone advise how to extract the Reply-To (I guess it is) address from the outgoing mail item?
It is quite bizarre that an Outlook recipient of this email will see
From: me#domain.com sent on behalf of bingo#bongo.com
but the SendOnBehalf property in the Outlook mail item simply reads "me#domain.com".
Any advice much appreciated. Thanks.
Try setting SentOnBehalf like this
Option Explicit
Sub SetSentOnBehalf()
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(0)
objMsg.SentOnBehalfOfName = "bingo#bongo.com"
objMsg.Display
MsgBox " SentOnBehalfOfName in the From: " & objMsg.SentOnBehalfOfName
Set objMsg = Nothing
End Sub

VB.net 2005 Sending Emails With Outlook 2003

We currently use the following code to create an email in Outlook so that the user can type what they want in Outlook, then when the email is sent, the system prompts them to see if they would like to save the email.
Dim objOutlook As Object
Dim objMessage As Object
Dim objInspector As Object
If strEMail <> "" Then
objOutlook = CreateObject("Outlook.Application")
objMessage = objOutlook.CreateItem(0)
objMessage.To = strEMail
objInspector = objMessage.GetInspector
objInspector.Display()
While Not objInspector.CurrentItem Is Nothing
End While
frmSaveSentEmail.BringToFront()
frmSaveSentEmail.ShowDialog()
The code works fine on Outlook 2003 as long as they are not using Word as their email editor. However, with Word set up as the email editor, the while loop that tests to see if the email object is closed never ends.
Is there a way to handle this differently so that it will work even with Word as the editor?
I am not terribly experienced with programming Outlook via VB.NET, but that loop certainly looks suspicious. Perhaps you should try taking advantage of the inspector's Close event instead of repeatedly checking its CurrentItem property. If I am not mistaken, you should be able to present your dialog within the event handler.
Ended up changing the loop to:
While Not objOutlook.ActiveInspector Is Nothing
End While
This resolved the issue.