Issue in selecting dynamic range in Outlook email body

Gaurangg

Board Regular
Joined
Aug 6, 2015
Messages
134
Hello Friends,

I am facing an issue while selecting the range of cells to paste them in email body. If I explain the issue in detail, I have set two columns F & G in which my data got pasted through another macro. Now the data may come in different count each time. sometimes in 1 row, sometimes 100 rows sometimes in 150 rows (i.e. F2:G2, F2:G100, F2:G32 etc...) so I have used end(xlUp) logic.. however eachtime only header got pasted instead of entire data. Whereas I want is.. whatever the data comes in numbers into F & G Columns.. it should pick the first headers till last filled row. Below is the code I am using. Please suggest where I should make the changes?

Code:
Sub Mail_Selection_Range_Outlook_Body()


Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object


Set rng = Nothing
' Only send the visible cells in the selection.


Set rng = Sheets("MailBody").Range("F1:G" & Rows.Count).End(xlUp)


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)




With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "Error Feedback Form"
    '.VotingOptions = "Accept;Dispute"
    .HTMLBody = RangetoHTML(rng)
    ' 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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    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  '****Here only F1 cell i.e. header got copied
    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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello Friends,

I am facing an issue while selecting the range of cells to paste them in email body. If I explain the issue in detail, I have set two columns F & G in which my data got pasted through another macro. Now the data may come in different count each time. sometimes in 1 row, sometimes 100 rows sometimes in 150 rows (i.e. F2:G2, F2:G100, F2:G32 etc...) so I have used end(xlUp) logic.. however eachtime only header got pasted instead of entire data. Whereas I want is.. whatever the data comes in numbers into F & G Columns.. it should pick the first headers till last filled row. Below is the code I am using. Please suggest where I should make the changes?

Rich (BB code):
Sub Mail_Selection_Range_Outlook_Body()


Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object

Set rng = Nothing
' Only send the visible cells in the selection.

Dim lastrow As Long
lastrow = Sheets("MailBody").UsedRange.Rows(Sheets("MailBody").UsedRange.Rows.Count).Row
Set Rng = Sheets("MailBody").Range("F1:G" & lastrow)


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)

With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "Error Feedback Form"
    '.VotingOptions = "Accept;Dispute"
    .HTMLBody = RangetoHTML(rng)
    ' 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

I put the code to get the last row
 
Upvote 0
Thanks a ton dear friend. This solution helped works perfectly. I sincerely apologized to reply you much late.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,181
Members
449,071
Latest member
cdnMech

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