Autofit in Outlook mail using Excel MailEnvelope - excel

I'm trying to send mail using Mail MailEnvelope but the content is wrapped after sending the mail like below image.
Sub Sample_MailEnvelope()
Application.ScreenUpdating = False
Sheets("Mail").Visible = True
Dim foliorange As Range
Set foliorange = Sheets("Countsheet").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
For Each mycell In foliorange
Worksheets("Mail").Unprotect (".")
Sheets("Mail").Range("A7:B7") = mycell.Offset(0, 2).Value
Sheets("Mail").Range("C7:D7") = mycell.Offset(0, 3).Value
Sheets("Mail").Range("E7:F7") = mycell.Offset(0, 4).Value
Dim Sendrng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Sheets("Mail").Activate
Range("A1").Select
Set Sendrng = Selection
With Sendrng
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
''.Introduction = "Hi," & vbNewLine & vbNewLine & "Kindly note that we have received the following transactions from you today." & vbNewLine & vbNewLine & vbNewLine & vbNewLine & vbNewLine
.Introduction = ""
With .Item
.To = mycell.Offset(0, 6).Value '"email#email.com"
.CC = mycell.Offset(0, 7).Value
.BCC = ""
.Subject = "OCBC - IUTA CONFIRMATION"
.Display
.send
End With
End With
End With
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = fasle
Next mycell
Worksheets("Mail").Protect "."
Sheets("Mail").Visible = False
Application.ScreenUpdating = True
End Sub
How to overcome this wrapping problem?
I tried to attach my sample macro file but I didn't find any option to attach files here.

Try something like below, modify as needed
Option Explicit
Public Sub Exampple()
Dim olApp As Object
Dim Email As Object
Dim Sht As Excel.Worksheet
Dim wdDoc As Word.Document
Set Sht = ActiveWorkbook.Sheets("Mail")
Dim rng As Range
Set rng = Sht.Range("A7:E7")
rng.Copy 'Picture Appearance:=xlScreen, Format:=xlPicture
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set olApp = CreateObject("Outlook.Application")
Set Email = olApp.CreateItem(0)
Set wdDoc = Email.GetInspector.WordEditor
With Email
.To = "email#email.com"
.Subject = "OCBC - IUTA CONFIRMATION"
.Attachments.Add ""
.Display
wdDoc.Paragraphs(1).Range.PasteAndFormat Type:=wdChartPicture
wdDoc.Paragraphs(1).SpaceAfter = 30
' if need setup inlineshapes hight & width
With wdDoc.InlineShapes(1)
.ScaleHeight = 113
.ScaleWidth = 114
End With
' .Display
.Send 'or use .Display
End With
Set wdDoc = Nothing
Set Email = Nothing
Set olApp = Nothing
End Sub
Make sure to Reference to Microsoft Word xx.x Object Library
https://stackoverflow.com/a/42662697/4539709

Related

How can I add text to an email with screenshot pic

I have the following code that sends a screenshot picture of a range> I would like to add text to the email but have not been able to figure out how.
Any help would be greatly appreciated.
'''
Public Sub ScreenShotResults2()
Dim rng As Range
Dim olApp As Object
Dim Email As Object
Dim Sht As Excel.Worksheet
Dim wdDoc As Word.Document
Set rng = Sheets("Summary").Range("B20:I34")
rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set olApp = CreateObject("Outlook.Application")
Set Email = olApp.CreateItem(0)
Set wdDoc = Email.GetInspector.WordEditor
'strbody = "See production data for most recent 3 months. "
With Email
.To = Worksheets("Summary").Range("B22").Value
.Subject = "4 Month LO Production Lookback for " & Worksheets("Summary").Range("B22").Value
'.HTMLBody = "<BODY style=font-size:12.5pt;font-family:Calibri>" & "</p>" & strbody & RangetoHTML(rng) & Signature
.Display
wdDoc.Range.PasteAndFormat Type:=wdChartPicture
'if need setup inlineshapes hight & width
With wdDoc
.InlineShapes(1).Height = 250
End With
.Display
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set Email = Nothing
Set olApp = Nothing
End Sub
There are several ways to add the text. Here is one:
'if need setup inlineshapes hight & width
With wdDoc
.InlineShapes(1).Height = 250
.Paragraphs.Add
.Paragraphs.Add
.Content.InsertAfter "Please look at the range image!"
End With
EDIT: here is an expanded example to add text before and after the image (without using RangeToHTML)
Option Explicit
Public Sub ScreenShotResults2()
Dim rng As Range
Set rng = Sheets("Summary").Range("B20:I34")
rng.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Dim olApp As Outlook.Application
Dim Email As Outlook.MailItem
Dim wdDoc As Word.Document
Set olApp = CreateObject("Outlook.Application")
Set Email = olApp.CreateItem(0)
Set wdDoc = Email.GetInspector.WordEditor
With Email
.To = Worksheets("Summary").Range("B22").Value
.Subject = "4 Month LO Production Lookback for " & _
Worksheets("Summary").Range("B22").Value
.Display
End With
With wdDoc.Content
'--- paste the range image first, because it overwrites
' everything in the document
.PasteAndFormat Type:=wdChartPicture
'--- now add our greeting at the start of the email
.InsertBefore "Dear Goober," & vbCr & vbCr & _
"See production data for most recent 3 months. " & _
vbCr & vbCr
'--- finally add our sign off after the image
.InsertAfter vbCr & vbCr & _
"This is my final comment." & vbCr & vbCr & _
"Sincerely," & vbCr & _
"Me!"
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set Email = Nothing
Set olApp = Nothing
End Sub

Sending Excel chart via mail (Outlook)

I found a code that turns a range of cell in Excel to a photo. That photo is delivered by mail. The problem is that when i'm using .Display everything is OK but when i'm using .Send the message sent empty.
Here is the code:
Sub Send_Pt_mail()
Dim OutApp As Object
Dim OutMail As Object
Dim Fname As String
Dim ch As ChartObject
'Prepare screen data file
Set ch = Worksheets("Chart").ChartObjects.Add(Range("Photo2Mail").Left, Range("Photo2Mail").Top, Range("Photo2Mail").Width, Range("Photo2Mail").Height)
'calculating the number of Recipients
iRow = Worksheets("Recipients").Cells(Rows.Count, 1).End(xlUp).Row
Recipients = ""
For i = 2 To iRow
'for each record in Recipients sheet an eMail will be send
If ThisWorkbook.Worksheets("Recipients").Cells(i, 2).Value = ThisWorkbook.Worksheets("Recipients").Cells(2, 7).Value Then
Recipients = Recipients & ThisWorkbook.Worksheets("Recipients").Cells(i, 1) & ";"
End If
Next i
'Prepare mail range as an image
Application.ScreenUpdating = True
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Fname = Environ$("temp") & "Mail_snap" & ".gif"
'select the relevant table (update or new data) and export through Chart to file
'then select the charts in dashboard and export through Chart 18 to file
ch.Chart.ChartWizard Source:=Worksheets("DB").Range("Photo2Mail"), gallery:=xlLine, Title:="New Chart"
' ch.Chart.ChartArea.ClearContents
' ch.Width = 1700
' ch.Height = 900
Chart_Name = ch.Name
Worksheets("DB").Activate
Range("Photo2Mail").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
Worksheets("Chart").ChartObjects(Chart_Name).Activate
ActiveChart.Paste
ActiveWorkbook.Worksheets("Chart").ChartObjects(Chart_Name).Chart.Export Filename:=Fname, FilterName:="gif"
S = "<img src=" & Fname & "><br>"
'On Error Resume Next
With OutMail
.To = Recipients
.CC = ""
.BCC = ""
.Subject = ThisWorkbook.Worksheets("Recipients").Cells(3, 4) & " " & Format(Now(), "dd/mm/yyyy")
.Save
.HTMLBody = S
' send
.display
End With
On Error GoTo 0
Kill Fname
ch.Delete
StopMacro:
Set OutMail = Nothing
Set OutApp = Nothing
Application.ScreenUpdating = False
If (ActiveWindow.Zoom <> 100) Then
ActiveWindow.Zoom = 100
End If
End Sub
If the mail body is not updated before sending then .GetInspector will act as .Display, except for not displaying. The idea is usually associated with generating default signatures especially when the flash associated with display is annoying.
Sub Send_With_Signature_Demo()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "myaddress#somewhere.com"
.Subject = Format(Now(), "dd/mm/yyyy")
' If you have a default signature
' you should find you need either .GetInspector or .Display
.GetInspector
.Save
.Send
End With
StopMacro:
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

pause excel VBA until user saves a copy of a file

What is the correct syntax for pausing a VBA until the user saves an excel attachment? In the VB below the user is prompted upon opening the workbook with a selection, if that selection is yes then another message box appears asking them to fill out a form and save. I am trying to pause the VB until save is clicked. However, I am getting many compile errors currently. The lines with a ** ** were added to try and accomplish thisThank you :).
VB
Private Sub Workbook_Open()
Dim WS As Worksheet, Rng As Range, c As Range
Dim OutApp As Object, OutMail As Object
Dim Msg As String, Addr As String, FName As String, i As Long
Dim obj As Object
**Dim MyDoc As Document**
Dim MyFileCopy As String
Dim intAnswer As Integer
'define path
MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Attachment.xlsx"
'open sheet
Sheets("Email").Activate
intAnswer = MsgBox("Are there any issues to report", vbYesNoCancel)
Select Case intAnswer
Case vbYes
Range("D2").Value = "x"
MsgBox ("Please select an issue and save"), vbExclamation
'create a separate sheet2 to mail out and pause VB
Sheets(2).Copy
Set wkb = ActiveWorkbook
With wkb
**Set MyDoc = Documents.Add
MyDoc.SaveAs "MyFileCopy.xlsx"
DoEvents
Do
Loop Until MyDoc.Saved
.Close True**
End With
Case vbCancel
Application.SendKeys "%{F11}", True
Case Else
Range("C2").Value = "x"
End Select
'create connection, check condition, send email
Set OutApp = CreateObject("Outlook.Application")
Set WS = ThisWorkbook.Sheets("Email")
With WS
Set Rng = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
End With
For Each c In Rng
Msg = "For " & WS.Cells(2, 2) & Chr(14) & Chr(14)
For i = 3 To 4
If LCase(WS.Cells(2, i)) = "x" Then
Msg = Msg & " -" & WS.Cells(1, i) & Chr(14)
End If
Next
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = c
.CC = ""
.BCC = ""
.Subject = "Daily Operational Safety Briefing"
.Body = Msg
If Range("D2").Value & Chr(14) = "x" Then .Attachments.Add MyFileCopy, 1
.Send
End With
Next c
Set OutMail = Nothing
Set OutApp = Nothing
'confirm message sent, clear sheet, and delete copy
MsgBox "The data has been emailed sucessfully.", vbInformation
Range("C2:D2").ClearContents
Kill MyFileCopy
'Exit and do not save
Application.Quit
ThisWorkbook.Close SaveChanges:=False
End Sub

vba to automatically email select sheet in spreadsheet if condition is met

I am trying out a couple different ways to automatically email using VBA and having trouble with the 2 items listed below. The VB does run as is but I would like to try to incorporate these two items if possible. Thank you :).
only attach sheet2(attachment). The file is located in bold in the code below.
only send the attachment if comment 2 is checked (cell D2)
VB
Private Sub CommandButton1_Click()
Dim WS As Worksheet, Rng As Range, c As Range
Dim OutApp As Object, OutMail As Object
Dim Msg As String, Addr As String, FName As String, i As Long
Dim obj As Object
Dim MyFile As String
**MyFile = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form.xlsm"**
If Dir(MyFile) = "" Then
MsgBox "The file to attach was not found here:" & vbLf & vbLf & MyFile, vbExclamation, "Exiting"
GoTo Abort
End If
On Error Resume Next
Set obj = GetObject(, "Outlook.Application")
On Error GoTo 0
If obj Is Nothing Then
Set obj = CreateObject("Outlook.Application")
End If
Set OutApp = CreateObject("Outlook.Application")
Set WS = ThisWorkbook.Sheets("Email")
Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
For Each c In Rng
Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
For i = 3 To 14
If WS.Cells(c.Row, i) = "x" Then
Msg = Msg & " -" & WS.Cells(1, i) & Chr(14)
End If
Next
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = c.Offset(, 0)
.CC = ""
.BCC = ""
.Subject = "Daily Operational Safety Briefing"
.Body = Msg
.Attachments.Add MyFile, 1
.Send
End With
MsgBox "The data has been emailed sucessfully.", vbInformation
Next c
Set OutMail = Nothing
Abort: Application.Quit
Set OutApp = Nothing
End Sub
Update
Dim MyFile As String, MyFileCopy As String
MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Sheet2.xlsm"
' no need to look for MyFile because you are working within it ...
'MyFile = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form.xlsm"
' If Dir(MyFile) = "" Then
'
' MsgBox "The file to attach was not found here:" & vbLf & vbLf & MyFile, vbExclamation, "Exiting"
' GoTo Abort
'
' End If
'create a separate sheet2 to mail out
Sheets(2).Copy
Set wkb = ActiveWorkbook
With wkb
.SaveAs MyFileCopy
.Close True
End With
'this is not needed, since you set the outlook app below
' On Error Resume Next
' Set obj = GetObject(, "Outlook.Application")
' On Error GoTo 0
' If obj Is Nothing Then
' Set obj = CreateObject("Outlook.Application")
' End If
Set OutApp = CreateObject("Outlook.Application")
Set WS = ThisWorkbook.Sheets("Email")
Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
For Each c In Rng
If c.Offset(, 3) = "x" Then 'Not sure how you have Comment2 "checked" in column D
Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
For i = 3 To 14
If WS.Cells(c.Row, i) = "x" Then
Msg = Msg & " -" & WS.Cells(1, i) & Chr(14)
End If
Next
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = c.Offset(, 0)
.CC = ""
.BCC = ""
.Subject = "Daily Operational Safety Briefing"
.Body = Msg
.Attachments.Add MyFileCopy, 1
.Send
End With
End If
Next c
MsgBox "The data has been emailed sucessfully.", vbInformation
Set OutMail = Nothing
Set OutApp = Nothing
Abort:
Application.Quit 'This will kill the Excel application, is this really what you want?
End Sub
See the below code. I placed a section to make a copy workbook with sheet2 to send as an attachment as well as added in the condition for D2 (assume column for each row) to check for the condition. See my comments, as I took some liberties without knowing how your exact data works. I also cleaned up some of the stuff that looked superfluous.
Private Sub CommandButton1_Click()
Dim WS As Worksheet, Rng As Range, c As Range
Dim OutApp As Object, OutMail As Object
Dim Msg As String, Addr As String, FName As String, i As Long
Dim obj As Object
Dim MyFile As String, MyFileCopy As String
MyFileCopy = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form Attachment.xlsm"
' no need to look for MyFile because you are working within it ...
'MyFile = "L:\NGS\HLA LAB\total quality management\QC & QA\DOSE reports\DOSE reporting form.xlsm"
' If Dir(MyFile) = "" Then
'
' MsgBox "The file to attach was not found here:" & vbLf & vbLf & MyFile, vbExclamation, "Exiting"
' GoTo Abort
'
' End If
'create a separate sheet2 to mail out
Sheets(2).Copy
Set wkb = ActiveWorkbook
With wkb
.SaveAs MyFileCopy
.Close True
End With
'this is not needed, since you set the outlook app below
' On Error Resume Next
' Set obj = GetObject(, "Outlook.Application")
' On Error GoTo 0
' If obj Is Nothing Then
' Set obj = CreateObject("Outlook.Application")
' End If
Set OutApp = CreateObject("Outlook.Application")
Set WS = ThisWorkbook.Sheets("Email")
Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
For Each c In Rng
If c.Offset(, 3) = "Checked" Then 'Not sure how you have Comment2 "checked" in column D
Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
For i = 3 To 14
If WS.Cells(c.Row, i) = "x" Then
Msg = Msg & " -" & WS.Cells(1, i) & Chr(14)
End If
Next
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = c.Offset(, 0)
.CC = ""
.BCC = ""
.Subject = "Daily Operational Safety Briefing"
.Body = Msg
.Attachments.Add MyFileCopy, 1
.Send
End With
End If
Next c
MsgBox "The data has been emailed sucessfully.", vbInformation
Set OutMail = Nothing
Set OutApp = Nothing
Abort:
Application.Quit 'This will kill the Excel application, is this really what you want?
End Sub

Outlook mail body is blank

I have the following to send an email, with a range of cells from my Excel sheet.
The email is sent with the correct Subject and CC.
There is data in that cell range (A:B) but I cannot get anything in the body. It stays blank.
Sub SendEmail()
SendEmail Macro
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Sheets("Test1").Range("F2").Value
.CC = Sheets("Test1").Range("F3").Value
.BCC = ""
.Subject = Sheets("Test1").Range("E1").Text
.Body = Sheets("Test1").Range("A:B")
.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
the error trapping you have is hiding the error: 13 - Type Mismatch
you will have to construct the .Body by looping through the values
Here's the code to loop through:
Dim I As Long
Dim LastRowColA As Long
Dim BodyString As String
BodyString = ""
LastRowColA = Sheets("Test1").Range("A65536").End(xlUp).Row
For I = 1 To LastRowColA
BodyString = BodyString & Sheets("Test1").Range("A" & I).Value & vbTab & Sheets("Test1").Range("B" & I).Value & vbCrLf
Next I
.Body = BodyString ' instead of = Sheets("Test1").Range("A:B")
Try changing
Sheets("Test1").Range("E1").Text
to
Sheets("Test1").Range("E1").Value

Resources