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
 
Hi,dk/Ivan,

How to combin these two code to one as i want to press a button to call a Macro to send Outlook email without ask users to select MS Outlook Object Libray and MS Scriptiong Runtime?

Tks.
CL
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Read the thread. It's answered fully in the last few pages - including how to reference the correct libraries through code. If you have any specific questions then please post back.
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

Hi,

I have tried the code on my machine and it displays a compile error(variable not defined) for the "xlSourceRange" inthe below line... weird. I am using office 97...

'Now create the HTML file
ActiveWorkbook.PublishObjects.Add(xlSourceRange, "C:tempsht.htm", rngeSend.Parent.Name, rngeSend.Address, xlHtmlStatic).Publish True
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

Hi mate,

This question was answered on the previous page - according to Andrew P the PublishObjects method wasn't added until Excel 2000, so I think you might have to find an alternative.
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

When the code copies the range to a .htm file it is not left aligend, it is in the centre, how can I change this
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

Hi,

This has been allowed for in the latest version of the code here.
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

Is there a part of the code that needs changing to make the copy left aligned. I have used the new code and it is still in the centre
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

Hi,

The code should left align as it is. I just tested on Excel 2000 and XP and it worked fine on my PC. Which version of Excel and Outlook are you using?

When you use this code does the range stay in exactly the same position, or does it move to the left at all?
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

I am using Excel and Outlook 2000,

The only thing in the range that changes is the amount of data which is copied.

I have did have to alter the code as it kept erroring, I had to change strTempFilePath to the file name otherwise it would say "bad file type"
 
Upvote 0
Re: Sending a selected range through Outlook - NOT A QUESTIO

I have got this working without having to replace anything, but it is still putting the text in the centre of the email???

I am adding some text to the strhtlmbody before and after the htm file is attached [see below]

strHTMLBody = "Please can you order the following:
" & oFSTextStream.ReadAll & " br Thankyou br" & "br" & Range("d3")

I have removed what I added but it is still going to the centre.
 
Upvote 0

Forum statistics

Threads
1,215,299
Messages
6,124,125
Members
449,142
Latest member
championbowler

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