Copy/Paste Range of cells as picture in outlook email with body - excel

what I want to do is copy a range of cells and paste them into an outlook template that also has a message in the body. The code that I have is
Sub SendEmail_Class_Name()
Dim r As Range
Set r = Range("A1:AA3")
r.Copy
Dim EmailApp As Outlook.Application
Dim Source As String
Set EmailApp = New Outlook.Application
Dim EmailItem As Outlook.MailItem
Set EmailItem = EmailApp.CreateItem(olMailItem)
EmailItem.To = "Name#gmail.com"
EmailItem.CC = "Name1#gmail.com; Name2#gmail.com;" & _
"Name3#gmail.com; Name4#gmail.com"
EmailItem.Subject = "Test Email From Excel VBA"
EmailItem.HTMLBody = "Hey,<p>" & "Seeing if this Macro will send an Email<p>" & _
"Thanks,<p>"
Source = ThisWorkbook.FullName
EmailItem.Attachments.Add Source
EmailItem.Display
Dim wordDoc As Word.Document
Set wordDoc = EmailItem.GetInspector.WordEditor
wordDoc.Range.PasteAndFormat wdChartPicture
End Sub
As of now the code will run and pull up the template with the To, CC, and Subject correct but the message in the body will not appear only the picture of the cells and I tried putting the picture before the code for the message and only the message will appear and not the picture. So, my question is how do I order my code so that both the message and picture appear in the body of the email.
Thanks

Related

Outlook freezes when sending from Excel

I'm really having trouble finding any answers for this problem. I have an Excel macro that filters a sheet (it's a basic order form), copies and emails a range using an Outlook object. The file worked for several weeks and ran quickly.
Now all of the sudden whenever the macro is run the Excel portion of filtering and copying works fine but when it gets to the email code Outlook locks up, and I get a popup from Excel saying it's waiting for Outlook to complete an OLE action. I end up having to kill the Outlook process. I've tried early and late bindings.
Sub EmailOrder()
Dim answer As Integer
Dim lastRow As String
Dim filteredRow
Dim emailApp As Outlook.Application
Dim emailItem As Outlook.MailItem
Dim exportRange As Range
Dim currentTime As String
Dim currentUserEmailAddress As String
answer = MsgBox("Click OK to send your order to the supply team", vbOKCancel)
If answer = vbOK Then
Worksheets("Sheet1").Activate
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
currentTable = "$A$1:$E$" & lastRow
'Filter out blanks
Range(currentTable).AutoFilter Field:=5, Criteria1:="<>"
Set exportRange = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set exportRange = Selection.SpecialCells(xlCellTypeVisible)
'Setup outlook objects and mail
Set emailApp = New Outlook.Application
Set emailItem = emailApp.CreateItem(olMailItem)
Set outSession = emailItem.Session.CurrentUser
currentUserEmailAddress = outSession.AddressEntry.GetExchangeUser().PrimarySmtpAddress
currentTime = Now
'Write email
With emailItem
.To = "redacted#gmail.com"
.CC = currentUserEmailAddress
.Subject = "Local Inventory Order " & currentTime
.HTMLBody = RangetoHTML(exportRange)
.Send
End With
'Close objects
Set emailApp = Nothing
Set emailItem = Nothing
MsgBox ("The order has been emailed to the supply team.")
End If
End Sub
The RangetoHTML function is from Ron de Bruin's website. Any help is appreciated.
EDIT: failed to mention that there have been other users of the sheet who reported it working for several weeks then stopping.

Outlook email without adjusting column width of pasted Excel range

I have code that takes a range of cells and pastes it to an Outlook email.
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim Email As Outlook.MailItem
Set Email = olApp.CreateItem(0)
Dim wdDoc As Word.Document '<=========
Set wdDoc = Email.GetInspector.WordEditor
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.Worksheets("Emails")
Dim rng As Range
Set rng = Sht.Range("A4:m51").SpecialCells(xlCellTypeVisible)
rng.Copy
With Email
.To = Sht.Range("C1")
.Subject = Sht.Range("b1")
.CC = Sht.Range("H1") & ";" & Sht.Range("H2")
.Display
wdDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
Exit Sub
I have a longer table, column wise, in the workbook.
When that table is converted into email format it changes the 'Preferred width' to '0.53'.
As soon as I unclick that radio button it resizes as it should be.
Is there a way to modify the code to not set a preferred width when its pasted into Outlook email?
try to add wddoc.Range.Tables(1).Columns.AutoFit at the end

How to add Excel ranges and text to email body, in specific positions?

I want to copy data from multiple ranges in an Excel sheet to an email body.
Below is the code I have come up with.
How to make ranges paste one under the other and how to add text after ranges but before signature from Outlook.
How it is now:
Sub reportCostLunch()
Dim recipient(0) As Variant
recipient(0) = ""
Dim outlook As Object
Dim email As Object
Dim xInspect As Object
Dim pageEditor As Object
Dim lastRow As Long
Dim sheet, sheet1 As Worksheet
Dim SDest As String, title As String, slot As String
Set sheet = ThisWorkbook.Sheets("SHEET1")
Set sheet1 = ThisWorkbook.Sheets("SHEET2")
title = sheet.Range("D13").Value
Set outlook = CreateObject("Outlook.Application")
Set email = outlook.CreateItem(0)
With email
SDest = ""
For i = LBound(recipient) To UBound(recipient)
If SDest = "" Then
SDest = recipient(i)
Else
SDest = SDest & ";" & recipient(i)
End If
Next i
.To = SDest
.Subject = title
.Display
Set xInspect = email.GetInspector
Set pageEditor = xInspect.WordEditor
Worksheets("SHEET2").Range("C44:AF71").Copy
pageEditor.Application.Selection.start = 1
pageEditor.Application.Selection.End = pageEditor.Application.Selection.start
pageEditor.Application.Selection.PasteAndFormat (wdChartPicture)
pageEditor.Application.Selection.InsertParagraphAfter
Worksheets("SHEET2").Range("C26:AF44").Copy
pageEditor.Application.Selection.PasteAndFormat (wdChartPicture)
pageEditor.Application.Selection.InsertParagraphAfter
.Display
email.HTMLBody = "SOME TEXT " _
& email.HTMLBody & " some text"
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set email = Nothing
Set outlook = Nothing
Application.CutCopyMode = False
End Sub
Create the Email Body in VBA.
Put it together into one variable, including all your paragraphs and line breaks. Then use that one variable for
email.HTMLBody = varEmailBody
Edit: If you use .HTMLBody you can't copy/paste anything. You need to construct the HTML as text somewhere (in your code). Ron de Bruin has excellent examples of all kinds of Excel to Outlook email scenarios. Here is one for mailing an Excel range in an HTML body: https://www.rondebruin.nl/win/s1/outlook/bmail2.htm -- You may need to construct one contiguous range in your worksheet (maybe on a hidden sheet) that you can process in the VBA as one range.

Office 2013 - VBA Email does not display To/CC/BCC variables

I am creating emails from Excel via the VBA Outlook.Application Reference. Each email is populated with data from my excel sheet and then placed into the To/CC/BCC/Subject/Body fields.
Now, when running this code in Office 2010 it works without a hitch, but in Office 2013 the variables containing the To/CC/BCC/etc. data does not show up in the actual email when displayed.
Did this reference change in Office 2013?
Sub MailSheet()
Dim OutApp As Object
Dim outMail As Object
Dim rng As Range
' set required variables
Set Sourcewb = ActiveWorkbook
Set Property = ActiveWorkbook.Sheets("Settings").Range("B4")
Set Holidex = ActiveWorkbook.Sheets("Settings").Range("B5")
Set SendTo = ActiveWorkbook.Sheets("Settings").Range("B29")
Set SendCC = ActiveWorkbook.Sheets("Settings").Range("B30")
Set rng = Sheets("Mail").Range("A1:F80")
' set email variables
Set OutApp = CreateObject("Outlook.Application")
Set outMail = OutApp.CreateItem(0)
' some code
' get ready to mail
With outMail
.To = SendTo
.ReplyRecipients.Add ""
.CC = SendCC
.BCC = ""
.Subject = Holidex & " - Daily Email"
.HTMLBody = RangetoHTML(rng)
' display email before sending
.Display '.Send or use .Display
End With
' some code
' Clean up
Set outMail = Nothing
Set OutApp = Nothing
end Sub
Rather than creating an Outlook object, try referencing the outlook library (Tools -> References and then select Microsoft Outlook xx.x Object Library). You can then reference it as below:
Sub SendAnEmail()
Dim oOlApp As Outlook.Application: Set oOlApp = Outlook.Application
Dim oMailItem As Outlook.MailItem: Set oMailItem = oOlApp.CreateItem(olMailItem)
oMailItem.To = "myemail#test.com"
oMailItem.CC = ""
oMailItem.BCC = "myemail#test.com"
oMailItem.Subject = Sheet1.Cells(15, "D")
oMailItem.HTMLBody = "Again .. testing"
oMailItem.Display
Set oMailItem = Nothing
Set oOlApp = Nothing
End Sub
You can either add this code in your sub or call this Sub from your Sub with parameters
Not sure I can help you directly, but I do have some code I found online which I know for a fact works with Outlook 2016, will share it on here in case it helps:
Sub OutlookMail_1()
'Automate Sending Emails from Excel, using Outlook.
'Send text and also contents from the host workbook's worksheet range
' as Mail Body, and add an attachment with the mail.
'Automating using Early Binding: Add a reference to the Outlook Object Library
' in Excel (your host application) by clicking Tools-References in VBE,
' which will enable using Outlook's predefined constants.
'Once this reference is added, a new instance of
' Outlook application can be created by using the New keyword.
'variables declared as a specific object type
' ie. specific to the application which is being automated:
Dim applOL As Outlook.Application
Dim miOL As Outlook.MailItem
Dim recptOL As Outlook.Recipient
Dim ws As Worksheet
Dim name As String
Dim email As String
Dim nominees As Range
Dim number As String
'set worksheet:
Set ws = ThisWorkbook.Sheets("Sheet1")
'Create a new instance of the Outlook application.
' Set the Application object as follows:
Set applOL = New Outlook.Application
'create mail item:
Set miOL = applOL.CreateItem(olMailItem)
'Add mail recipients, either the email id or their name in your address book.
' Invalid ids will result in code error.
Set recptOL = miOL.Recipients.Add("Main recipient email")
recptOL.Type = olTo
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc
'with the mail item:
With miOL
'subject of the mail:
.Subject = "Subject"
'Chr(10) represents line feed/new line, & Chr(13) represents carriage return.
' Send text and also contents from
' the host workbook's worksheet range as Mail Body.
.Body = "BODY OF EMAIL"
'set importance level for the mail:
.Importance = olImportanceHigh
'add an attachment to the mail:
'send the mail:
.Display
End With
'clear the object variables:
Set applOL = Nothing
Set miOL = Nothing
Set recptOL = Nothing
End Sub
Some of the variables I set are redundant because I edited the code slightly to maintain privacy, but let me know if that helps!
If you want to use CC instead of Bcc, then just change the code to:
recptOL.Type = olcc

Pasting an Excel range into an email as a picture

I'm creating an Outlook email from Excel (Office 2013). I want to paste a range of cells (C3:S52) into the email as a picture.
Below is the code I have so far. Where am I going wrong?
Sub Button193_Click()
'
' Button193_Click Macro
'
'
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("C3:S52").Select
Selection.Copy
End Sub
Sub CreateMail()
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach As Range
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
With ActiveSheet
Set rngTo = .Range("E55")
Set rngSubject = .Range("E56")
Set rngBody = .Range("E57")
End With
With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.Body = rngBody.Value
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
End With
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing
End Sub
Sub Button235_Click()
'
' Button235_Click Macro
'
'
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("A1:M27").Select
Selection.Copy
End Sub
Sub RunThemAll()
Application.Run "Button193_Click"
Application.Run "CreateMail"
End Sub
Here's a worked example, tested in Office 2010:
'Copy range of interest
Dim r As Range
Set r = Range("B2:D5")
r.Copy
'Open a new mail item
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim outMail As Outlook.MailItem
Set outMail = outlookApp.CreateItem(olMailItem)
'Get its Word editor
outMail.Display
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor
'To paste as picture
wordDoc.Range.PasteAndFormat wdChartPicture
'To paste as a table
'wordDoc.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
Result:
In the code above I used early binding to have access to autocomplete; to use this code you need to set references to the Microsoft Outlook and Microsoft Word object libraries: Tools > References... > set checkmarks like this:
Alternatively, you can forget about the references and use late binding, declaring all the Outlook and Word objects As Object instead of As Outlook.Application and As Word.Document etc.
Apparently you're having trouble implementing the above; the range pastes as a table rather than a picture in your email message. I have no explanation for why that would happen.
An alternative is then to paste as an image in Excel, and then cut and paste that image into your e-mail:
'Copy range of interest
Dim r As Range
Set r = Range("B2:D5")
r.Copy
'Paste as picture in sheet and cut immediately
Dim p As Picture
Set p = ActiveSheet.Pictures.Paste
p.Cut
'Open a new mail item
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim outMail As Outlook.MailItem
Set outMail = outlookApp.CreateItem(olMailItem)
'Get its Word editor
outMail.Display
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor
'Paste picture
wordDoc.Range.Paste
As pointed out by WizzleWuzzle, there is also the option of using PasteSpecial instead of PasteAndFormat or Paste...
wordDoc.Range.PasteSpecial , , , , wdPasteBitmap
... but for some reason, the resulting image doesn't render as well. See how the lower table is kind of blurry:
I am providing an alternative solution to the above problem as Outlook.MailItem.GetInspector.WordEditor does not work in some organizational environments.
For security purposes, the HTMLBody, HTMLEditor, Body and WordEditor properties all are subject to address-information security prompts because the body of a message often contains the sender's or other people's e-mail addresses. And, if Group Policy does not permit then these prompts do not come on-screen. In simple words, as a developer, you are bound to change your code, because neither registry changes can be made nor group policy can be modified.
Hence, if your code suddenly stopped working after migrating to Office 365 or for any other reasons, please refer to the code below. Comments have been added for easy understanding and implementation.
If you have administrative rights then try the registry changes given at below link:
https://support.microsoft.com/en-au/help/926512/information-for-administrators-about-e-mail-security-settings-in-outlo
However, as developer, I recommend a code that's rather compatible with all versions of Excel instead of making system changes because system changes will be required on each end user's machine as well.
Code Compatible: Excel 2003, Excel 2007, Excel 2010, Excel 2013, Excel 2016, Office 365
Option Explicit
Sub Create_Email(ByVal strTo As String, ByVal strSubject As String)
Dim rngToPicture As Range
Dim outlookApp As Object
Dim Outmail As Object
Dim strTempFilePath As String
Dim strTempFileName As String
'Name it anything, doesn't matter
strTempFileName = "RangeAsPNG"
'rngToPicture is defined as NAMED RANGE in the workbook, do modify this name before use
Set rngToPicture = Range("rngToPicture")
Set outlookApp = CreateObject("Outlook.Application")
Set Outmail = outlookApp.CreateItem(olMailItem)
'Create an email
With Outmail
.To = strTo
.Subject = strSubject
'Create the range as a PNG file and store it in temp folder
Call createPNG(rngToPicture, strTempFileName)
'Embed the image in Outlook
strTempFilePath = Environ$("temp") & "\" & strTempFileName & ".png"
.Attachments.Add strTempFilePath, olByValue, 0
'Change the HTML below to add Header (Dear John) or signature (Kind Regards) using newline tag (<br />)
.HTMLBody = "<img src='cid:DashboardFile.png' style='border:0'>"
.Display
End With
Set Outmail = Nothing
Set outlookApp = Nothing
Set rngToPicture = Nothing
End Sub
Sub createPNG(ByRef rngToPicture As Range, nameFile As String)
Dim wksName As String
wksName = rngToPicture.Parent.Name
'Delete the existing PNG file of same name, if exists
On Error Resume Next
Kill Environ$("temp") & "\" & nameFile & ".png"
On Error GoTo 0
'Copy the range as picture
rngToPicture.CopyPicture
'Paste the picture in Chart area of same dimensions
With ThisWorkbook.Worksheets(wksName).ChartObjects.Add(rngToPicture.Left, rngToPicture.Top, rngToPicture.Width, rngToPicture.Height)
.Activate
.Chart.Paste
'Export the chart as PNG File to Temp folder
.Chart.Export Environ$("temp") & "\" & nameFile & ".png", "PNG"
End With
Worksheets(wksName).ChartObjects(Worksheets(wksName).ChartObjects.Count).Delete
End Sub

Resources