MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 14th, 2002, 12:19 PM   #1
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,682
Default Sending a selected range through Outlook - NOT A QUESTION!

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 ]
dk is offline   Reply With Quote
Old May 14th, 2002, 09:08 PM   #2
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,208
Default

DK

Excellent......Have copied this code.

The only thing I can add is to automatically
referece the code Libraries.
This is something I'll setup for other users
JUST IN CASE.....

__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 15th, 2002, 04:59 AM   #3
crimlet
 
Join Date: May 2002
Location: Ipswich, Suffolk, England
Posts: 135
Default

Sorry, but when using win 98 xl 97 how do I reference OL express ect...
crimlet is offline   Reply With Quote
Old May 15th, 2002, 05:19 AM   #4
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,682
Default

Sorry Crimlet, but this code will only work with Outlook, not OE. Unfortunately it's not even possible to convert the code to work properly with OE as it doesn't support automation (which is what this code basically is doing).

Regards,
Dan
dk is offline   Reply With Quote
Old May 15th, 2002, 05:33 AM   #5
crimlet
 
Join Date: May 2002
Location: Ipswich, Suffolk, England
Posts: 135
Default

Sorry, I did mean outlook as apposed to OL express
crimlet is offline   Reply With Quote
Old May 15th, 2002, 06:39 AM   #6
Tom Schreiner
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Bother Ivan later about the code to reference the library in the procedure it'self....
For now, open your VBE and click on Tools, References, then find Microsoft Outlook 8.0 Object Library, check it and click Ok.
This will set a reference to the Library you need.
Tom
Tom Schreiner is offline   Reply With Quote
Old May 15th, 2002, 10:31 AM   #7
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,208
Default

Quote:
On 2002-05-15 01:39, TsTom wrote:
Bother Ivan later about the code to reference the library in the procedure it'self....
For now, open your VBE and click on Tools, References, then find Microsoft Outlook 8.0 Object Library, check it and click Ok.
This will set a reference to the Library you need.
Tom
You also need to reference the SCRRUN.DLL
But to automatically set it up then;

EDIT: using Juan's Addin

Option Explicit

Dim ID As Object

Const sGuid As String = " Referencing ["
Const sMsg As String = "You may have to manually set a reference to the ["

Sub CreateRef_Library_ScFso()
'// Set a Reference to SCRRUN.DLL = Scripting.FileSystemObject
'// Microsoft scripting Runtime
'// If it already exits Resume & Handle latter!

On Error Resume Next

Set ID = ThisWorkbook.VBProject.References

ID.AddFromGuid "{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0
**
If Err.Number <> 32813 And Err.Number <> 0 Then
****ErrMsg Err.Number, Err.Description, Err.HelpFile, Err.HelpContext, _
********"Microsoft scripting Runtime"
End If

End Sub

Sub CreateRef_Library_Outlook()
'// Set a Reference to Microsoft Outlook M.m Object Library = msoutlM.olb
'// Microsoft Outlook M.n Object Library
'// If it already exits Resume & Handle latter!
On Error Resume Next

Set ID = ThisWorkbook.VBProject.References
ID.AddFromGuid "{00062FFF-0000-0000-C000-000000000046}", 7, 0
If Err.Number <> 32813 And Err.Number <> 0 Then
****ErrMsg Err.Number, Err.Description, Err.HelpFile, Err.HelpContext, _
********"Microsoft Outlook Object Library"
End If

End Sub

Sub ErrMsg(En, Ed, Eh, Ehc, ss)
'// Error Object handler
MsgBox "Error Number:= " & Err.Number & vbCr _
********& "Error Discrp:= " & Err.Description & vbCr & vbCr & sMsg & ss & "]", _
********vbMsgBoxHelpButton, _
********"Error" & sGuid & ss & "]", Err.HelpFile, Err.HelpContext
Set ID = Nothing

End Sub
__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 15th, 2002, 11:05 AM   #8
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,682
Default

Ivan,

I like this code very much. I was aware that you can add references 'on-the-fly' but have never tried it. I've got a few of questions:-

1. Where do you find the GUID number? Is it in the registry?

2. Does the GUID take account of different versions of software? e.g. will this code install a reference to Outlook regardless of its version?

3. How comes the AddFromGUID method doesn't appear when you type it into the object browswer? If I set a reference to the VBA extensibility library I can find it - do you know what's going on here?

Thanks,
Dan
dk is offline   Reply With Quote
Old May 15th, 2002, 07:22 PM   #9
eddy
 
Join Date: Mar 2002
Posts: 459
Default

Yipeeeeeeeeeeeeeeeeeee!!!
Thanks to everyone for this code. I have been looking to do just this for ages. It seems to work fine with any combination of - WIN98/2000/XP & Office 2000/2002.
Thanks also to TSTom for letting me know the thread was here.
One happy bunny!
eddy is offline   Reply With Quote
Old May 15th, 2002, 07:37 PM   #10
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,771
Default

Hey dk, great code ! I would only add one thing to it. Kill the sht.html file after... Just a little detail !
__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:16 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2010 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes