I created a macro in Excel to send emails to various users every time a specific file is updated.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As String
answer = MsgBox("Would you like to save the changes?", vbYesNo, "Save Document")
If answer = vbNo Then Cancel = True
If answer = vbYes Then
'open outlook type stuff
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'add recipients
'newmsg.Recipients.Add ("Name1")
newmsg.Recipients.Add ("email#xxx.com")
'newmsg.Recipients.Add ("Name2")
newmsg.Recipients.Add ("email#xxx.com")
'add subject
newmsg.Subject = "Notification - Update file"
'add body
newmsg.Body = "This is an automated notification." & vbNewLine & vbNewLine & _
"The XXX file has been recently updated" & vbNewLine & vbNewLine & _
"Please do not reply to this email."
newmsg.Display 'display
newmsg.Send 'send message
'give conformation of sent message
MsgBox "Your document has successfully been saved", , "Confirmation"
End If
'save the document
'Me.Worksheets.Save
End Sub
I would like to add a hyperlink to the body text where it says "The XXX file has been recently updated" so that XXX file is a clickable link to a website.
The Outlook object model supports three main ways of customizing the message body:
The Body property returns or sets a string representing the clear-text body of the Outlook item.
The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create e-mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Note, the MailItem.BodyFormat property allows you to programmatically change the editor that is used for the body of an item.
The last two supports creating a hyperlink in the message body. It is up to you which way is to choose.
If you want to do that, you'll have to write HTML instead of plain text.
This line:
newmsg.Body = "The XXX file has been recently updated"
... would become something like:
newMsg.HTMLBody = "The XXX file has been recently updated".
This is because in Outlook emails with formatting you write HTML text, and a link in HTML is expressed as follows:
your Hyper-text
Related
I have code to populate email from Excel using VBA. I want to retain previous correspondence and also add my signature to the end of the email.
Code that opens the current Outlook item and populates it. The variable ChsBody is assigned earlier.
If OutItem.Class = olMail Then
Set replyEmail = OutItem.ReplyAll
With replyEmail
.Signature
.Body = ChsBody
.Display
End With
Else
MsgBox "ERROR: Make sure email is selected"
End If
Would it be possible to retain the email chain and add my response as a string?
Would it be possible to retain the email chain and add my response as a string?
Change
.Body = ChsBody
to
.HTMLBody = ChsBody & "<br>" & .HTMLBody
In the code you override the Body content with your own:
.Body = ChsBody
Instead, if you want to preserve the existing content, you need to append the string:
.Body = ChsBody & .Body
Note, there are three main ways of dealing with message bodies in Outlook:
Body.
HTMLBody.
The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body.
See Chapter 17: Working with Item Bodies for more information.
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
Updated per suggestions from Jeeped:
I am looking for a method of creating a set of emails fitting the following parameters:
each email will be personalized to the recipient and based off a template letter set by my supervisor.
There will be a greeting line with their name and title, along with the names of the departments they oversee.
each email will have a set of documents specific to that recipient.
they should be saved to file for final inspection before they are sent.
column 5 that is not referenced in the code below is the column containing the department name.
The closest I have come is the following code:
Sub send_template_w/attachments()
On Error Resume Next
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.Mailitem
Set omail =.Createitem(olMailitem)
Dim I As Long
For i=2 To Range(“a100”).End(xlUp).Row
With omail
.Body = “Dear “ & Cells(i,1).Value
.To = Cells(i,2).Value
.CC = Cells(i,3).Value
.Subject = Cells(i,4).Values
.Attachments.Add Cells(i,6)
.Attachments.Add Cells(i,7)
.SaveAs Environ("HOMEPATH") &; "\My Documents\" & Cells(i,2).Value
End With
Next
End Sub
So far, this code will generate and save an email but what I want to do is use a present email template for these emails--either by adding the greeting at the beginning and department name into the body of the the email to be sent out. Can this be done through a word or Outlook document and if so, how?
Create a model of the mail. "Save As" to an .oft file. For example MyTemplate.oft
Instead of
Set omail =.Createitem(olMailitem)
there is
Set omail = o.CreateItemFromTemplate("C:\MyTemplate.oft").
To add the entries from the Excel sheet you could include unique placeholders in the body of the template then Replace with Excel values.
I have created a form for users to fill out and submit. The idea is that once submit is clicked on it immediately emails the completed excel form as an attachment to a specific email address.
But what actually happens is that it sends the email without the attachment. Can you help please, the VBA currently in place is
Option Explicit
Private Sub CommandButton1_Click()
On Error GoTo ErrHandler
' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.To = "temi.akinnaike#asos.com"
.Subject = "Issue Resolution Request"
.Body = "Hi Finance Systems, please see the attached issues log for your attention"
.Send ' SEND THE MESSAGE.
.Save
'Add the active workbook as an attachment
.Attachments.Add "C:\Temp\SLA & addnl details form 3 - without VBA for flie attachment.xlsm"
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
'
End Sub
Edit your code as follows:
With objEmail
.To = "temi.akinnaike#asos.com"
.Subject = "Issue Resolution Request"
.Body = "Hi Finance Systems, please see the attached issues log for your attention"
.Attachments.Add "C:\Temp\SLA & addnl details form 3 - without VBA for flie attachment.xlsm"
.Send ' SEND THE MESSAGE.
.Save
End With
The issue is that you are adding the attachment after you are sending the mail.
Also note that this issue may be caused by a typographical mistake in the parameters for the .Attachments.Add option?
.Attachments.Add "C:\Temp\SLA & addnl details form 3 - without VBA for flie attachment.xlsm"
Maybe it should be
"...file attachment.xlsm"
When you click reply all in outlook, it will open a new email box, but show the previous email thread underneath the body of your email. I am trying to Reply All to the selected email in outlook and input information. I want to do this all from a Macro in Excel. My problem is that if I try to write in the body of the Reply All, it erases the entire previous email thread.
Sub test()
Dim mail 'object/mail item iterator
Dim replyall 'object which will represent the reply email
For Each mail In Outlook.Application.ActiveExplorer.Selection
If mail.Class = olMail Then
Set replyall = mail.replyall
With replyall
'.Body = "blah blah hello world" '<-- uncomment and it will delete the thread
.Display
End With
End If
Next
End Sub
Note: the closest I've come is this, but it deletes my signature, the email separator, and the header info from the latest email:
.Body = "blah blah hello world" & mail.body
Any solutions are appreciated! Thanks!
For the plain text, you can do
.Body = "blah blah hello world" & vbCrLf & .Body
If you want to preserve formatting, you will need to insert your string into the replyall.HTMLBody property (you cannot just concatenate two HTML strings).