Help Debugging Code : Placing a range in the body of an email.

JamesGilchrist

New Member
Joined
Apr 12, 2011
Messages
13
Hi All,

Topic - Help Debugging Code : Placing a range in the body of an email.

Am not programmer, I am cobbling together this and dont have the expertise to debug it! Can some one PLEASE help?

HTML:
Sub CreateEmail()

'Clears a range that may cause a bug
Range("A501").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=-500

'selects the range that I need placed in the body of an email WORKS GREAT
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("B4:R" & LastRow).Select

'NOT MY CODE...this opens an email but the range selected in
'the previous step does not get placed into the body of the email.

Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
 
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
 
    Set rng = Nothing
    On Error Resume Next
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    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
 
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
 
    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Display   'or use .Send
    End With
    On Error GoTo 0
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
 
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Included with your original code above in the same VBA module, do you also have the code for the Function RangetoHTML ? You need it if you don't.

What line does it error on?

Also, don't forget to change the email "To:" address in the code...
.To = "ron@debruin.nl"
 
Last edited:
Upvote 0
Hi AlphaFrog,

I have put the RangetoHTML in....This code does not bugg out a highlight an error. It just doesn't deliver the desired result.

It opens up an email, but does not place the selection in the body.

Any help is appreciated.
 
Upvote 0
As a test to see if the range is converted to HTML, add this MsgBox line to your code. The HTML code for the range should then be displayed in a message box.
Code:
    Set rng = Nothing
    On Error Resume Next
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    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
    [COLOR="Red"]MsgBox RangetoHTML(rng)[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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