How to add inline pdf document using vba? - excel

I am trying to create an Outlook email draft with an inline pdf document.
I managed to add inline pictures using html img src tag but this does not work for documents. What should I modify to add pdf instead of images?
I tried using the position but does not add in the correct position instead adds to the end of text.
Set outlook = createObject(“Outlook.Application”)
Set mailItem = outlook.CreateItem(olMailItem)
With mailItem
.BodyFormat = olFormatRichText
.Body = “hello world”
.Attachments.add “file.pdf”, olByValue, 6
End With

You can only do that in the RTF format, not in HTML. When calling MailItem.Attachments.Add, specify the Position parameter appropriately.

It seems like the add function only works after you call display, else it will only add to the end of the entire body
Set outlook = createObject(“Outlook.Application”)
Set mailItem = outlook.CreateItem(olMailItem)
With mailItem
.BodyFormat = olFormatRichText
.Body = “hello world”
.Display
.Attachments.add “file.pdf”, olByValue, 6
End With

Related

Is it possible to have an excel macro open outlook in browser and populate the fields of a new message as well as attach a file?

Question: Is it possible to create a macro that opens outlook in a web browser and populates the fields of a new message as well as attach a file? The outlook portion of the current macro only opens outlook in a browser.
ActiveWorkbook.FollowHyperlink Address:="https://outlook.office365.com/mail/**shared mailbox address**"
Background: I am trying to update an excel macro that currently saves a pdf of the sheet, opens the outlook application, fills out the necessary fields and attaches the saved pdf to the email. This macro has worked fine, but we have recently moved to using a shared mailbox to send the message. Now the users have encountered problems sending from the shared mailbox using the outlook application. The solution is to use outlook in the browser (edge), but the macro I currently have can only open outlook in the browser and requires the user to fill out all the fields and find and attach the saved pdf. There have been problems with this and I was hoping there was a way to automate the process like our old macro would.
Old macro:
Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)
On Error Resume Next
With NewMail
.To = ReportName
.CC = ""
.Subject = TempFileName
.Body = ""
.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
Well, Yes and No.
Yes, you can get VBA to do this... but No, it won't be remotely as easy as running it through the desktop application.
A workaround that still uses desktop application, is to
Give all users that need to send the email access to this inbox from their own desktop apps.
Use the ".SendUsingAccount" property
Sub ExampleSub()
Dim OLApp As Object
Dim NewMail As Object
Set OLApp = CreateObject("Outlook.Application")
Set NewMail = OLApp.CreateItem(0)
On Error Resume Next
With NewMail
.SentOnBehalfOfName = "MySecondaryAddress#Domain.com"
.To = "someaddress#gmail.com"
.CC = ""
.Subject = "Example Subject"
.Body = "Good Morning..."
'.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Display '.Send or use .Display to show you the email before sending it.
End With
On Error GoTo 0
End Sub

How can I reply to email without losing the previous email chain?

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.

Need to send separate emails to multiple recipients from excel list

I have an excel list of people. I want to send emails to all as separate mails. Need to dynamically change the subject,body and recipients.
I have tried using vba and doing it. But i do not know how to dynamically change the subject and body. Also how can i enter multiple lines in the body?
i do not want all recipients to be sent the same mail.Image shows the field names in excel
I need to have first name and first letter of last name in subject. And in body first line includes Hi firstname..And personal mail goes in the "to" field and professional as "cc"
Try it this way:
'Initialize objects
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = ws.Range("A1") 'Assuming TO mail addresses are located here and separated with ";"
.CC = ws.Range("B1") 'Assuming TO mail addresses are located here and separated with ";"
.Subject = ws.Range("C1") 'Assuming subject is declared here
.HTMLBody = ws.Range("D1") 'Assuming body is declared here
If address_attachment_line.Value <> "" Then
.Attachments.Add FilePath & FileName
End If
.Display
End With
Through storing the dynamic information in the ranges referenced by the code, you can control the single mails.
Furhermore to make linebreaks in the body text, just use the tag <br> as it is interpreted as HTML content.
Hope this helps you!

Trying to create personalized emails with multiple attachments

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.

Embed an image in the body of an email

I send emails via Outlook using ".HTMLBody = ..." to customize body.
I tried to add these lines to add an image that I named "logo" in the worksheet:
With Sheets("Mail - GREF COMMITTEE AGENDA")
.Shapes("logo").Export "C:\Users\Public\Pictures\logo.png"
End With
'...
.HTMLBody = bodyEn & bodyFR & "< img src='C:\Users\Public\Pictures\logo.png'>"
The error is:
438 Object doesn't support this method or property.
Update: Any way to embed the image in the mail is helpful. I can export the image manually.
Use Word's InlineShapes.AddPicture Method
The procedure is described in Working with Item Bodies.
Inserting pictures:
strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
objSel.InlineShapes.AddPicture strFile, False, True
End If

Resources