Send email from today's date - excel

I have an excel spreadsheet for permits, and one column is their expiration dates. My boss wants to be emailed about the permits that expire in the next 2 weeks. How can I use visual basic to tell Outlook to send an email each time he opens the excel spreadsheet?
This is for a spreadsheet I set up, permit names are in column A, dates are in column J.
Sub Mail_small_Text_Outlook()
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"Cell A1 is changed" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
I may have to use a button to run the program, but I'm not sure if it is possible to schedule emails.

It appears that you have not dimmed OutApp as an object.
Once that is done add a loop examining the expiration dates and adding the specific cells to a dynamic array, re-dimming the array with each iteration. Finally each license on the array will need to be added to a text string that can then be added to the body of the email you are creating above....
Once you have all the above working as you would like, you will want to tie the whole operation to the Workbook Open event.

In the VBE, open This Workbook...
Then use the drop-down menus to select the Workbook and Open Event and add your code and save.

Related

Add location and bcc/resource - Don't Update Location

I've written a macro in Excel to send calendar invites (appointments) via Outlook. The recipients must be bcc'd (added to Resources field).
I have text in the body of the calendar appointment. It appears that by utilizing the WordEditor in combination with bcc/resources, I get an alert pop-up before each send: "Do you want to update the location to...?"
I do not want to update/change the location, as it would get replaced by the recipient list, thus defeating the reason for bcc (recipients would see Location as the entire recipient list).
If I remove the code block that adds text to the body (starting with "Set ActInsp..."), then this alert does not appear, and everything else works correctly; however, I need the text body with a hyperlink.
gif of how to duplicate the "Update Location" alert manually.
Below is a working sample of the macro. The code block with WordEditor appears toward the bottom, right above .Display.
Be sure to add the Reference: Microsoft Outlook 16.0 Object Library (I failed to get late binding to work).
Sub SendAppointments_SingleEmail()
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
'Requires early binding (late binding not working):
' Go to the Tools menu, Resources. Add Microsoft Outlook 16.0 Object Library
'Because AppointmentItem does not use HTML, must utilize Word VBA
Dim ActInsp As Outlook.Inspector
'Static fields
emailFrom = "test#gmail.com"
emailSubject = "My Subject"
emailBody = "Body of calendar invite"
hyperlink = "https://www.register.com/"
emailLocation = "My Location"
appt_Date = #7/30/2019#
appt_Time = #3:00:00 PM#
appt_Duration = "90"
'Create Appointment and Send
Set myAppt = olApp.CreateItem(olAppointmentItem)
With myAppt
.MeetingStatus = olMeeting
.SendUsingAccount = emailFrom
.Subject = emailSubject
.Location = emailLocation
.Start = appt_Date & " " & appt_Time
.Duration = 90
Set myResourceAttendee = .Recipients.Add("test1#test.com")
myResourceAttendee.Type = olResource 'Add as a Resource/BCC
Set ActInsp = myAppt.GetInspector
With ActInsp
.WordEditor.Characters(1).InsertBefore (emailBody & vbNewLine & vbNewLine & hyperlink)
.Close (olSave)
End With
.Display
'.Send
End With 'myAppt
End Sub
Instead of Closing the Object from ActInsp, Close the myAppt object.
So change this part of your code:
With ActInsp
.WordEditor.Characters(1).InsertBefore (emailBody & vbNewLine & vbNewLine & hyperlink)
.Close (olSave)
End With
.Display
'.Send
With:
With ActInsp
.WordEditor.Characters(1).InsertBefore (emailBody & vbNewLine & vbNewLine & Hyperlink)
'.Close (olSave)
End With
.Display
.Close (olSave)
'.Send

How to workaround a pop up classification when sending mail?

I send an email with VBA. A classification is popped up for each email and it needs to be set by hand. I am trying to work around this in the code.
I found a code to send emails: Mail a message with outlook via VBA.
After fixing few things, the following code is working.
Sub sendEmail()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Application.EnableEvents = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please Finish your course " & Cells(cell.Row, "C") & _
" before expiry date."
.Send 'Or use Display
End With
On Error GoTo 0
Set OutMail = Nothing
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
The problem is that after sending emails from the list to for example 10 persons, I need to click on classification pop up 10 times.
I found this: How to save workbook and handle TITUS (or any other document classification add-in) popup?
I tried .EnableEvents = False before .Send. I am not sure if this does serve me.
How to use this in my case? Is it doable to disable it, work around it, or even set a classification within the code?
There is a workaround, but you have to do it in Outlook Developer itself. You can set up an event handler in Outlook which triggers a macro. So, in this case, Outlook could watch for a message to be created with a specific subject line (as an example), and THAT would trigger the script below, which bypasses TITUS.
'Sets Titus Mail settings and sends mail
With AOMailMsg
objMsg.ItemProperties.Add("ABCDE.Registered To", olText) = "My Companies"
objMsg.ItemProperties.Add("ABCDE.Classification", olText) = "Internal"
objMsg.UserProperties.Add("ABCDE.Registered To", olText) = "My Companies"
objMsg.UserProperties.Add("ABCDE.Classification", olText) = "Internal"
objMsg.UserProperties.Add("TITUSAutomatedClassification", olText) = _
"TLPropertyRoot=ABCDE;.Registered To=My Companies;.Classification=Internal;"
objMsg.Send
End With

How to send Automated Email when specific Excel cell value reached due date

I've excel sheet (invoice) , i am updating client invoice and bill status regularly , i need to send remainder email to my client before bill reaches credit date (before 7 days) (only Pending bills), i am manually sending the email so far to my clients,
is there any option that automated email can send from excel before due date reaches (7days before and 3 days before) , could any one help me
Email Format
Excel Sheet Sample
This should do what you want!
To run a macro automatic when you manual change a specific cell you can use the Change event in a worksheet module.The example on this page use Cell A1 and will run the macro if the cell value >200.
1) Right click on a sheet tab and choose view code
2) Paste the event below in the sheet module.
3) Alt-q to go back to Excel
Note: Change YourMacroName to the name of your macro in the code.
If you want the code to work for another cell or more cells you can change the range in the event.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 200 Then
Call YourMacroName
End If
End If
End Sub
Example mail macro
Test this example macro to create/display a Outlook mail with a small text message.
You must copy this macro in a standard module and not in the worksheet module, see this page how.
Note: I use .Display in the code to display the mail, you can change that to .Send
Do not forget to change Call YourMacroName to Call Mail_small_Text_Outlook in the Change event.
Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi there" & vbNewLine & vbNewLine & _
"Cell A1 is changed" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = strbody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
http://www.rondebruin.nl/win/s1/outlook/bmail9.htm

Automate trigger from email that has been replied

I am new in VBA. I would like to ask on how to trigger email which has been reply.
Scenario : I have this coding as below which send the email to recipient (Column B) if there is "yes" in column C.
For Each cell In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "A").Value _
& vbNewLine & vbNewLine & _
"Please contact us to discuss bringing " & _
"your account up to date"
'You can add files also like this
'.Attachments.Add ("C:\test.txt")
.Send 'Or use Display
End With
Question : How can I trigger if the recipient has replied to my email that I sent earlier? I would like to automate the trigger to my excel file on column E as remark recipient has replied to my email. Ex, "replied / no reply".
Really appreciate for any help since I am new in VBA.
Thank you.
Assuming your using Microsoft Outlook and an Exchange Server.
There are 3 Extended MAPI properties that deal with the message state for replied to/forwarded:
PR_ICON_INDEX (0x10800003)
PR_LAST_VERB_EXECUTED (0x10810003)
PR_LAST_VERB_EXECUTION_TIME (0x10820040)
This MSDN article https://msdn.microsoft.com/en-us/library/bb176395(office.12).aspx provides code that shows how to use these MAPI Properties:
Sub DemoPropertyAccessorGetProperty()
Dim PropName, Header As String
Dim oMail As Object
Dim oPA As Outlook.PropertyAccessor
'Get first item in the inbox
Set oMail = _
Application.Session.GetDefaultFolder(olFolderInbox).Items(1)
'PR_TRANSPORT_MESSAGE_HEADERS
PropName = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"
'Obtain an instance of PropertyAccessor class
Set oPA = oMail.PropertyAccessor
'Call GetProperty
Header = oPA.GetProperty(PropName)
Debug.Print (Header)
End Sub
You will want to replace the 'PR_TRANSPORT_MESSAGE_HEADERS ie 0x007D001E in the above code and I'm guessing you'll want to go through more than just the first mail item...

Detecting User Choice with VBA Outmail application in Excel

I'm trying to figure out a way to detect whether or not the user clicks "Send" in the Outlook application that is displayed. I've tried reading the value of .Display similarly to how one would detect user input when using the FileDialog application (someInt = .Show), to no avail. I can't find any documentation on the Outmail Application, so any help would be greatly appreciated.
Set olApp = CreateObject("Outlook.Application")
Set Outmail = olApp.CreateItem(olMailItem)
With Outmail
.To = clientEmail
.CC = projectManagerEmail
.BCC = ""
.Subject = projectName & " (PO # " & poNumber & ", Job #" & projectNumber & ") - " & fileType & " (" & fileName & ")"
.Attachments.Add ActiveWorkbook.Path & "\" & fileType & "\" & folderName & "\" & fileName & ".pdf"
.Display
.Save
End With
I believe you need to intercept the Send operation in Outlook.
In Outlook, go to VBA Editor (Alt-F11), then paste below into the ThisOutlookSession under Microsoft Outlook Objects.
Make sure your operations works in Outlook, then close Outlook. You may have to Sign the code, change Macro Security Settings depending on your environment. Value of Cancel is what determines if the user has clicked Send (e.g. clicked -> Cancel=False).
Since there is no direct way to get the value of Cancel, may be you have to create a unique text file in local temp folder and pick it up in Excel to indicate it is Sent.
Private Sub Application_ItemSend(ByVal oItem As Object, Cancel As Boolean)
' Add Operations or Sub calls here
MyCheck01 oItem, bCancel
End Sub
Private Sub MyCheck01(ByVal oItem As Object, Cancel As Boolean)
' Do operations here. If Send is to be aborted, set Cancel to True.
End Sub
You will also need to define this olMailItem in Excel (Const olMailItem = 0).

Resources