Following macro works fine in newer version of excel, however get compile error in 2010

1nk3d

Board Regular
Joined
May 31, 2016
Messages
51
The below macro triggers a compile error in excel 2010. I am not great with macros, so any insight would be appreciate. The macro copies part of a workbook and pastes it as a picture in the body of an email and sends it.
Code:
Sub FRIMail()
Dim r As Range
Set r = Worksheets("Fri").Range("A1:M69")
r.Copy
Dim OutApp As Object
    Dim outMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set outMail = OutApp.CreateItem(0)
On Error Resume Next
    With outMail
    .display
    .HTMLBody = activemailmessage.HTMLBody
        .To = Worksheets("Distribution").Range("N13").Value
        .CC = ""
        .BCC = ""
        .Subject = "REPORT"
        .body = "Report was run on: " & Now

Dim wordDoc As Word.document
Set wordDoc = outMail.GetInspector.WordEditor

wordDoc.Range.PasteAndFormat wdChartPicture
outMail.send
End With
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
.
Your GO TO resource for email via VBA : https://www.rondebruin.nl/win/s1/outlook/mail.htm

Here is some code to accomplish your task :

Code:
Option Explicit
Sub CopyRows()
Dim i As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
    ws1.Range("A2:D14").Copy
    Mail_Selection_Range_Outlook_Body
End Sub


Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim lEndRow
Dim Value As String
Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("Sheet1").Range("A2:D14")
If rng Is Nothing Then
    MsgBox "An unknown error has occurred. "
    Exit Sub
End If
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .to = "Your email address here in quotes"
    .CC = ""
    .BCC = ""
    .Subject = "Trigger Point for Cars On Hand"


    .HTMLBody = "Text above Excel cells" & "

" & _
                RangetoHTML(rng) & "

" & _
                "Text below Excel cells.
"
    
    ' 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
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
    'Close TempWB
    TempWB.Close savechanges:=False
    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Last edited:
Upvote 0
I believe this one I tried but it would just send a blank email.... I could add the .display ahead of it?
 
Upvote 0
.
Use of .Display instead of .Send allows the email to be made visible for review prior to sending. I personally believe that to be a safer
means of sending email .... last chance to back out of a bad mistake. Of course, if the purpose is 'mass mailing' I wouldn't want to review
each email in that circumstance.

The macro works here. Just be certain to check your sheet name and range indicators .. that they are correct.

I noticed your posted macro did not include the HTML Function code. That is necessary to do what you are attempting.
 
Last edited:
Upvote 0
I’m on mobile away from computer at the moment I will try it tomorrow. I just recall having to add . Display at the beginning and .send at the end for it to actually paste the cell range as an image. I need it as an image as the cell range I’m working with, I have a lot of hidden cells with formulas. For example the range would be A1:AB35, but from H-Z is hidden cells with formulas, poor planning on my part really. Will it paste as shown, or will it paste all hidden cells?
 
Upvote 0
.
I had never tried the macro with a hidden cell ... so this is the result of my experiment:

I hid column C :


[TABLE="class: head"]
<tbody>[TR]
[TH][/TH]
[TH]
A​
[/TH]
[TH]
B​
[/TH]
[TH]
D​
[/TH]
[/TR]
[TR]
[TD]

[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A2[/TD]
[TD]B2[/TD]
[TD]D2[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A3[/TD]
[TD]B3[/TD]
[TD]D3[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A4[/TD]
[TD]B4[/TD]
[TD]D4[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A5[/TD]
[TD]B5[/TD]
[TD]D5[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A6[/TD]
[TD]B6[/TD]
[TD]D6[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A7[/TD]
[TD]B7[/TD]
[TD]D7[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A8[/TD]
[TD]B8[/TD]
[TD]D8[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A9[/TD]
[TD]B9[/TD]
[TD]D9[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A10[/TD]
[TD]B10[/TD]
[TD]D10[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A11[/TD]
[TD]B11[/TD]
[TD]D11[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A12[/TD]
[TD]B12[/TD]
[TD]D12[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A13[/TD]
[TD]B13[/TD]
[TD]D13[/TD]
[/TR]
[TR]
[TD]

[/TD]
[TD]A14[/TD]
[TD]B14[/TD]
[TD]D14[/TD]
[/TR]
</tbody>[/TABLE]

It shows Col C in the email, but the spacing between Col C and Col D is less :


[TABLE="width: 192"]
<tbody>[TR]
[TD]A2[/TD]
[TD]B2[/TD]
[TD]C2 D2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A3[/TD]
[TD]B3[/TD]
[TD]C3 D3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A4[/TD]
[TD]B4[/TD]
[TD]C4 D4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A5[/TD]
[TD]B5[/TD]
[TD]C5 D5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A6[/TD]
[TD]B6[/TD]
[TD]C6 D6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A7[/TD]
[TD]B7[/TD]
[TD]C7 D7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A8[/TD]
[TD]B8[/TD]
[TD]C8 D8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A9[/TD]
[TD]B9[/TD]
[TD]C9 D9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A10[/TD]
[TD]B10[/TD]
[TD]C10 D10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A11[/TD]
[TD]B11[/TD]
[TD]C11 D11[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A12[/TD]
[TD]B12[/TD]
[TD]C12 D12[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A13[/TD]
[TD]B13[/TD]
[TD]C13 D13[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A14[/TD]
[TD]B14[/TD]
[TD]C14 D14[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
.
I cleaned up the code some :

Code:
Sub CopyRows()
Dim i As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
    ws1.Range("A2:D14").Copy
    Mail_Selection_Range_Outlook_Body
End Sub




Sub Mail_Selection_Range_Outlook_Body()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim lEndRow
Dim Value As String
Set rng = Nothing
' Only send the visible cells in the selection.
Set rng = Sheets("Sheet1").Range("A2:D14")
If rng Is Nothing Then
    MsgBox "An unknown error has occurred. "
    Exit Sub
End If
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .to = "Your email address here in quotes"
    .CC = ""
    .BCC = ""
    .Subject = "Trigger Point for Cars On Hand"




    .HTMLBody = "Text above Excel cells" & "" & RangetoHTML(rng) & "" & "Text below Excel cells."


    
    ' 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
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
    'Close TempWB
    TempWB.Close savechanges:=False
    'Delete the htm file we used in this function
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Also, understand that the term .DISPLAY refers only to having the email shown before it is sent. If you use .DISPLAY like so :

Code:
  ' In place of the following statement, you can use ".Display" to
    ' display the e-mail message.
    .Display

which comes after your TO / FROM / SUBJECT / HTML BODY / ATTACHMENT ... it gives you an opportunity to review the email before you manually click on the SEND
button.

If you use .SEND instead of .DISPLAY, the email is automatically sent without you ever viewing it before it is sent.
 
Upvote 0
Is there a way to not have C in the email? Lots of formulas there, maybe copy and paste in a separate sheet, and paste into email and delete sheet? Just trying to think of other ways, as the hidden cells will CO fuse the recepients
 
Upvote 0
.
There is probably a better way of doing this ... I admit this isn't the "prettiest" way but it does work :

Replace the CopyRows macro with this one :

Code:
Sub CopyRows()
Dim i As Integer
Dim lastRow As Variant
Dim copyRange As Range
Dim src As Worksheet
Set src = ThisWorkbook.Sheets("Sheet1")
src.Columns("C").Cut
src.Columns("Z").Insert
Set copyRange = Sheets("Sheet1").Range("A:D" & lastRow)
    src.Range("A:D").Copy
    Mail_Selection_Range_Outlook_Body
src.Columns("C").Insert
src.Columns("Z").Cut
src.Columns("C").Insert
src.Columns("D").Delete
End Sub

The above presumes your Column Z will not have any data in it. The column can always be changed to something else.
 
Last edited:
Upvote 0
So for this area for C, would I just add a new section underneath for each column indicating what goes? For example if the code was for A:F, and I wanted C and E gone, add one underneath replacing the Cs with Es? I recall an issue due to me having some merged cells, will this work for that? If I wanted the sheet to be pasted not as specials but as it is with the color headers and such can that be accomplished?

Code:
src.Columns("C").Insert
src.Columns("Z").Cut
src.Columns("C").Insert
src.Columns("D").Delete
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top