Sending a selected range through Outlook - NOT A QUESTION!

dk

MrExcel MVP
Joined
Feb 15, 2002
Messages
2,942
Good afternoon all,

I've seen a few posts recently asking about sending either sending a range or a worksheet in an Outlook email as the body of the message through code. I've been looking at this and think I've come up with something that might work. I'd appreciate it if any of you XL kings and queens would take a look and see if the code works OK on your machine. I've sent a few messages to myself (sad I know :) ) and they seem to work well.

Here's the code. You need to set a reference to the Outlook object Library AND the Microsoft Scripting Runtime in order for this code to work.

Any ideas for improvement, suggestions, comments gratefully received.

Dan

Code:
Option Explicit

Sub SendRange()

'Sends a specified range in an Outlook message and retains Excel formatting

'Code written by Daniel Klann 2002

'References needed :
'Microsoft Outlook Object Library
'Microsoft Scripting Runtime


'Dimension variables
Dim olApp As Outlook.Application, olMail As Outlook.MailItem
Dim FSObj As Scripting.FileSystemObject, TStream As Scripting.TextStream
Dim rngeSend As Range, strHTMLBody As String


'Select the range to be sent
On Error Resume Next
Set rngeSend = Application.InputBox("Please select range you wish to send.", , , , , , , 8  )
If rngeSend Is Nothing Then Exit Sub    'User pressed Cancel
On Error GoTo 0

'Now create the HTML file
ActiveWorkbook.PublishObjects.Add(xlSourceRange, "C:tempsht.htm", rngeSend.Parent.Name, rngeSend.Address, xlHtmlStatic).Publish True


'Create an instance of Outlook (or use existing instance if it already exists
Set olApp = CreateObject("Outlook.Application")

'Create a mail item
Set olMail = olApp.CreateItem(olMailItem)

'Open the HTML file using the FilesystemObject into a TextStream object
Set FSObj = New Scripting.FileSystemObject
Set TStream = FSObj.OpenTextFile("C:tempsht.htm", ForReading)

'Now set the HTMLBody property of the message to the text contained in the TextStream object
strHTMLBody = TStream.ReadAll

olMail.HTMLBody = strHTMLBody

olMail.Display


End Sub
This message was edited by dk on 2002-05-14 07:21
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Ok, I am pretty dense here . . . What real-life purpose does this code serve? Why not just send the workbook to someone; or copy the data you want to send, and paste into a new workbook and send? I am sure there is a useful purpose . . . just not seeing it . . . . thanks!
 
Upvote 0
Hey all. Newbie here...couple questions: am i going to paste that code in excel? and how do I get this to work with outlook? Did I mention newbie?? If somebody could please give me specific instructions on how to do this it would be greatly appreciated. everything from telling outlook who to send it to in my address book. This is exactly what i've been looking for! please help!
 
Upvote 0
Hello and welcome to the board!

This is quite an old post and with so many posts it's difficult to tell what exactly you are trying to do.

This website has plenty of methods described for sending mail.

BTW: Usually we suggest that you start your own thread rather than piggy-back off another.
 
Upvote 0
Hi All,

New to VB and need your help to complete an assignment. The codes which are written over in this thread would work on Office 2003? Also if this works in 2003 please tell me the exact way it should be copied as i believe there are many subjects and dont know which one should start first.

Thanks in Advance.
 
Upvote 0
Hi,

Is it possible to send the data in ranges in 2 different sheets of a excel.

I am able to send one range of sheet1 using the above code.I need to add some range in sheet1 and another range of data in sheet 2 .

I cant put the data 2 sheets in same sheet as the column width and other formatting is entirely different.

Any help would be helpful.

Thanks
Vasanth
 
Upvote 0

Forum statistics

Threads
1,215,312
Messages
6,124,192
Members
449,147
Latest member
sweetkt327

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