Copy All Visible(Formatted Text) from Excel to Outlook using VBA? - excel

Copy All Visible(Formatted Text) from Excel to Outlook using VBA?
Please find below code to send email through outlook. However, I want to send the email with a selection that I have copied below using code.
I do not wish to create a Table as HTML but instead just copy all visible?
Sub EmailRep()
Dim Mailbody As Range
Application.DisplayAlerts = False
Dim Outlook As Outlook.Application
Set Outlook = CreateObject("Outlook.Application")
Dim outmail As MailItem
Set outmail = Outlook.CreateItem(0)
Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30")
Mailbody.Copy
With outmail
.To = "abc#xyz.com"
.Subject = "All Open"
.Body = "This is Test Email"
.Display
.Send
End With
Set Outlook = Nothing
Set outmail = Nothing
Set Mailbody = Nothing
End Sub

If I understand correct change your line of :
Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30")
To
Set Mailbody = ActiveWorkbook.Worksheets("Dashboard").Range("A1:F30").SpecialCells(xlCellTypeVisible)
Although in your code you are not putting the range into the body of the email. At first thought you pasted the range by hand but then I noticed you have .Send in code which would send the email before you got a chance to paste.
Either way the above will copy only the visible range.
If you are interested in a quick way to send your range in an email without the need to copy the below is pretty short and sweet:
Sub EmailRep()
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Range("A1:F30").SpecialCells(xlCellTypeVisible).Select
ActiveWorkbook.EnvelopeVisible = True
With ActiveSheet.MailEnvelope
.Introduction = "This is Test Email"
.Item.To = "abc#xyz.com"
.Item.Subject = "All Open"
.Item.Send
End With
ActiveWorkbook.EnvelopeVisible = False
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub

Related

Pasting to Outlook from Excel - Outlook Screen Updating

I have found the code to paste a chart from excel to outlook here on stack over flow.
This works fine but The issue is the outlook creating new email and pasting procedure is getting displayed on the screen. Is there any way to disable or make this to background?
Sub Mail_Range()
Dim Sht As Excel.Worksheet
Set Sht = ThisWorkbook.ActiveSheet
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim rng As Range
Set rng = Sht.Range("A5:W20")
rng.Copy
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Dim OutMail As Object
Set OutMail = OutApp.CreateItem(0)
Dim vInspector As Object
Set vInspector = OutMail.GetInspector
Dim wEditor As Object
Set wEditor = vInspector.WordEditor
With OutMail
.TO = "xxx.xxx.com"
.CC = ""
.Subject = Sht.Range("A5").Value
.GetInspector
wEditor.Paragraphs(1).Range.Text = "This is an auto generated e-mail" & vbCr
wEditor.Paragraphs(2).Range.Paste
.send
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Application.CutCopyMode = False
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
When I'm using
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
after outlook application creation my pasting code doesn't work. Im getting an email with no content.
Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")
With Application <<<---- change to OutApp
.ScreenUpdating = False
.EnableEvents = False
End With
If your problem is that you don't want to see the whole procedure of the mail, but turning the ScreenUpdating to false shows you a blank image, here is my code for dealing this:
Call AhorroMemoria(False)
Imagen.CopyPicture xlScreen, xlBitmap
With wsM.ChartObjects.Add(Imagen.Left - Imagen.Left * 0.15, Imagen.Top - Imagen.Top * 0.15, _
Imagen.Width - Imagen.Width * 0.15, Imagen.Height - Imagen.Height * 0.15)
.Activate
wsM.Shapes("Gráfico 1").Line.Visible = msoFalse
.Chart.Paste
.Chart.Export wb.Path & "\" & Servicio & Contador & ".jpg", "JPG"
End With
Call AhorroMemoria(True)
Call AhorroMemoria(False) turns on everything, screenupdating, enablevents and so... I do that just when copying the image as you can see on the code, then I turn it all off again on the Call AhorroMemoria(True).
Hope it helps.
Thanks all for helping me out. All your codes helped in some way. But i have found more simpler code from Microsoft here. I'm not sure which versions will support this and it has any other challenges. For now this works for me in Office 2016.
I'm getting an email envelope for brief amount of time in excel but no issues as the accidental edit can't be done in this method. In the original method accidental edit was possible during the paste function is running.
Also this code seamlessly emailing charts on the excel sheet.
Option Explicit
Sub Send_Range()
' Select the range of cells on the active worksheet.
ActiveSheet.Range("A1:B5").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = True
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To and Subject lines. Finally the message
' is sent.
With ActiveSheet.MailEnvelope
.Introduction = "This is a sample worksheet."
.Item.To = "E-Mail_Address_Here"
.Item.Subject = "My subject"
.Item.Send
End With
End Sub
I think you can not suppress display of e-mail creation screen display by invoking Word Editor approach. If you go through previous SO Posts and comments of experienced experts it gets amply clear that you can not suppress display of e-mail creation screen display.
In order to totally disable e-mail creation screen display please take reference from programs at roundebruin which covers all types of possibilities of sending emails without displaying email creation screen. Here is a slight variation to your code which works for me and posted, if someone finds it useful for similar situation.
Public Sub Emails()
Dim str As String
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
str = ws1.Range("A5").Value
With newEmail
.To = "xxx.xxx.com"
.CC = ""
.BCC = ""
.Subject = str
.body = ""
.display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
'Set ws1 = ThisWorkbook.Worksheets("Sheet1")
ws1.Range("A5").Copy
pageEditor.Application.Selection.Paste xlValues
ws1.Range("A5:W20").Copy
pageEditor.Application.Selection.Paste xlValues
.send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub

Create an email with a meeting invitation attached

I am working on an Excel workbook to track appointments and to send an email to advise of the appointment.
I put together code to open an email in Outlook with inputs pulled from the Excel sheet.
Sub ButtonREMINDER41_Click()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Selection.SpecialCells(xlCellTypeVisible)
Set rng =
Sheets("Sheet1").RangeToHtml("D4:D12").SpecialCells(xlCellTypeVisible, xlTextValues)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
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 = Range("$F$41")
.CC = Range("$B$41")
.BCC = ""
.Subject = "Upcoming Scheduled Appointment"
.HTMLBody = Range("$N$41")
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
I am trying to also book an appointment in the receiver's calendar.
I would like an email generated with a meeting attachment for the appointment.

How to reach the "send to mail recipient"

I need to automate a report, this is automated almost completely, but in the end of the process it has a specific sheet to be send by email, and they ask for a special format.
I found code that will send the email, but it does not do the same thing that I do when using it manually, "send to mail recipient" button on the Excel ribbon.
This is the code that I am using:
Dim aSheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sendrng = Worksheets("for_Mail_test").Range("A1:j42")
Set aSheet = ActiveSheet
With Sendrng
.Parent.Select
Set rng = ActiveCell
.Select
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
.Introduction = "This is test mail 2."
With .Item
.To = "ron#debruin.nl"
.CC = ""
.BCC = ""
.Subject = "My subject"
.Display
End With
End With
rng.Select
End With
aSheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
There are two problems:
1 it does not keep the same format that I am on the sheet
2 when I change the method from .send to .display it does not showup

Send an email, if a cell in Excel has a specific date

Excel will have a list of different dates and receivers' email ids across each date.
If the cell has a specific date, an auto-email will be sent to the email ids mentioned across it.
The mail is to be sent through Outlook.
Pretty general question but lets first talk about the possible approach.
1. Get date from cells in a loop
2. If date matches with a particular date, call a sub to send mail
3. Pass corresponding mail ids in parameter (of the sendMail method) to send mail
I won't be able to give you exact code but here is roughly how it will be:
For i = 1 to n
strDate = workbooks.worksheet("Sheet1").cells(i,1)
emailList = workbooks.worksheet("Sheet1").cells(i,2)
if (strDate=expectedDate) then
call sendMail(emailList)
end if
Next
Public function sendMail(emailList)
Dim OutApp As Object
Dim OutMail As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
On Error GoTo err
'Now open a new mail
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = emailList
.CC = ""
.Subject = "test"
.Body = "the content of the mail"
End With
On Error GoTo 0
'set nothing to the objects created
Set OutMail = Nothing
Set OutApp = Nothing
'Now set the application properties back to true
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox ("Email has been Sent Successfully")
Exit Sub
err:
MsgBox err.Description
End function

Direct VBA to save a copy of a spread sheet in a designated folder in outlook

I have code to take a copy of a select worksheet but am haveing difficulties directing which draft folder in outlook to send the draft email to. The name of the folder I want to send the draft email to is "Draft NDIC". Here is the code:
Sub Mail_Body_NDIC()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set rng = Nothing
Set rng = Sheets("NDIC Renewals").UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "dvandervieren#enerplus.com"
.CC = ""
.BCC = ""
.Subject = "NDIC Renewals for the Next 90 Days"
.Body = ""
.HTMLBody = RangetoHTML(rng)
.Save 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
"You need to use the .Move method, with the olDestFolder as the argument." David Van der Vieren
http://blog.saieva.com/2010/03/27/move-messages-to-folders-with-outlook-vba/

Resources