Solved

EMail Using Outlook the Specified Content in the spreadsheet at a click of button using VBA

Posted on 2011-02-20
9
480 Views
Last Modified: 2012-05-11
I have a CommandButton in the spreadsheet, at a click of that, i want to copy the specified range of cell content into body of an EMail

EX:
I want to copy the Cells range from B2:G20, into an Email body at a click of button
0
Comment
Question by:srikanthv2322
  • 5
  • 4
9 Comments
 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
Ron de Bruin covers this in detail here

http://www.rondebruin.nl/mail/folder3/mailenvelope.htm

This line
   Set Sendrng = Worksheets(1).Range("B2:G20")

Says copy B20:g20 from sheet 1

Cheers

Dave
Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
    Dim AWorksheet As Worksheet
    Dim Sendrng As Range
    Dim rng As Range

    On Error GoTo StopMacro

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the Worksheet/range you want to mail
    'Note: if you use one cell it will send the whole worksheet
    Set Sendrng = Worksheets(1).Range("B2:G20")

    'Remember the activesheet
    Set AWorksheet = ActiveSheet

    'Create the mail and send it
    With Sendrng

        ' Select the worksheet with the range you want to send
        .Parent.Select

        'Remember the ActiveCell on that worksheet
        Set rng = ActiveCell

        'Select the range you want to mail
        .Select

        ' Create the mail and send it
        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "This is a test mail."

            ' In the "With .Item" part you can add more options
            ' See the tips on this Outlook example page.
            ' http://www.rondebruin.nl/mail/tips2.htm
            With .Item
                .To = "ron@debruin.nl"
                .Subject = "My subject"
                .Send
            End With

        End With

        'select the original ActiveCell
        rng.Select
    End With

    'Activate the sheet that was active before you run the macro
    AWorksheet.Select

StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False

End Sub

Open in new window

0
 

Author Comment

by:srikanthv2322
Comment Utility
I want the Code to just open an New Email using Outlook, fill up the Subject of the line and Body of an Email with specified range of cells
its shouldn't keep anyone's email in To list or send that Email
0
 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
If you remove these two lines (as below), then the SendMail envelope (from inside Excel) is displayed with the email address, subject, range etc

.Send
ActiveWorkbook.EnvelopeVisible = False



Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
    Dim AWorksheet As Worksheet
    Dim Sendrng As Range
    Dim rng As Range

   ' On Error GoTo StopMacro

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the Worksheet/range you want to mail
    'Note: if you use one cell it will send the whole worksheet
    Set Sendrng = Worksheets(1).Range("B2:G20")

    'Remember the activesheet
    Set AWorksheet = ActiveSheet

    'Create the mail and send it
    With Sendrng

        ' Select the worksheet with the range you want to send
        .Parent.Select

        'Remember the ActiveCell on that worksheet
        Set rng = ActiveCell

        'Select the range you want to mail
        .Select

        ' Create the mail and send it
        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "This is a test mail."

            ' In the "With .Item" part you can add more options
            ' See the tips on this Outlook example page.
            ' http://www.rondebruin.nl/mail/tips2.htm
            With .Item
    
                .To = "ron@debruin.nl"
                .Subject = "My subject"
            End With

        End With

        'select the original ActiveCell
        rng.Select
    End With

    'Activate the sheet that was active before you run the macro
    AWorksheet.Select

StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With

End Sub

Open in new window

0
 

Author Comment

by:srikanthv2322
Comment Utility
Its copying the entire worksheet, including even the command buttons as well

I need only specified cell contents only, please help me out
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
Comment Utility
Ok while the first sample worked ok for me pls try this adpation that uses the Outlook model


Again code is Ron's
http://msdn.microsoft.com/en-us/library/ff519602%28v=office.11%29.aspx

This line sets the range to be copied to the temp file which is then added to the email
   Set rng = Sheets(1).Range("b20:g20")

Cheers

Dave
Sub Mail_Selection_Range_Outlook_Body()
' You need to use this module with the RangetoHTML subroutine.
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
    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 = Sheets(1).Range("b20:g20")
    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 = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .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)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
    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 workbook to receive the data.
    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 an .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 the RangetoHTML subroutine.
    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.
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Open in new window

0
 

Author Comment

by:srikanthv2322
Comment Utility
Please refer the attached file for screen shot, i am not able to copy the specified range of cells , instead its copying the entire sheet Send-To-Email.doc
0
 

Author Comment

by:srikanthv2322
Comment Utility
It should just open an New Email and copy the range of cells intended, instead if i close teh Email template its closing the entire Excel sheet itself
0
 
LVL 50

Expert Comment

by:Dave Brett
Comment Utility
> Please refer the attached file for screen shot, i am not able to copy the specified range of cells , instead its copying the entire sheet
I posted the wrong version
   Set Sendrng = Worksheets(1).Range("B2:G20")
should have been
   Set Sendrng = Worksheets(1).Range("B20:G20")
in #a34940338.html

But pls try the more recent Outlook code I posted in
#a34940460.html
0
 

Author Comment

by:srikanthv2322
Comment Utility
Awesome Brettdj...thanks a million
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Resolve DNS query failed errors for Exchange
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now