I have initially answered question How to only paste visible cells in an email body
The code I tested and posted (see below) did not include sending email. After the OP added that to his question, I added the .Send
part, but the behavior I obtained is very strange.
If I place a breakpoint just before sending, and I execute the Sub
, an email is created with the correct info (including the pasted Excel Range
). Then I continue execution and the email is correctly sent. But if I run the whole Sub
at once, with no breakpoints, the email is sent with no pasted Excel Range
in the body.
What is the reason for that, and what is the solution?
I have tried commenting/uncommenting the two last lines (Set ... = Nothing
), but it does not help.
Related questions:
Copy range of cells from Excel to body of email in Outlook
Pasting formatted Excel range into Outlook message
Code of reference (based on the quintessential code by Ron de Bruin, see this and this):
Sub SendEmail()
Dim OutlookApp As Object
'Dim OutlookApp As Outlook.Application
Dim MItem As Object
'Dim MItem As Outlook.MailItem
'Create Outlook object
Set OutlookApp = CreateObject("Outlook.Application")
'Set OutlookApp = New Outlook.Application
Dim Sendrng As Range
Set Sendrng = Worksheets("Test").Range("A1").SpecialCells(xlCellTypeVisible)
Sendrng.Copy
'Create Mail Item
Set MItem = OutlookApp.CreateItem(0)
'Set MItem = OutlookApp.CreateItem(olMailItem)
With MItem
.To = "test@email.com"
.To = "SSEREBRINSKY@TENARIS.COM"
.Subject = "Test"
.CC = ""
.BCC = ""
'.Body = "a"
.Display
End With
SendKeys "^({v})", True
With MItem
.Send
End With
'Set OutlookApp = Nothing
'Set MItem = Nothing
End Sub