Hello
I have this macro below that I made from various sources (including here).
It generates an outlook email from a range in excel.
It successfully works, but there's one last detail i'm trying to figure out.
When I simply create a new email and copy/paste my excel range manually in the body of the email, the row heights are the same as in the original excel file.
But when I use the macro instead, the row height in the email is too big (0.53cm instead of approx 0.3cm).
So the time I save copy/pasting my stuff manually, I loose resizing the rows.
Does anyone know how to solve this?
Using Excel & Outlook 2007 on Windows Vista pro
Many thanks!
I have this macro below that I made from various sources (including here).
It generates an outlook email from a range in excel.
It successfully works, but there's one last detail i'm trying to figure out.
When I simply create a new email and copy/paste my excel range manually in the body of the email, the row heights are the same as in the original excel file.
But when I use the macro instead, the row height in the email is too big (0.53cm instead of approx 0.3cm).
So the time I save copy/pasting my stuff manually, I loose resizing the rows.
Does anyone know how to solve this?
Using Excel & Outlook 2007 on Windows Vista pro
Many thanks!
Code:
Sub preparemail()
Dim OutApp As Object
Dim OutMail As Object
Dim Ash As Worksheet
Set Ash = ActiveSheet
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "me@me.com"
.Subject = "test"
.HTMLBody = RangetoHTML(Sheets("Overview").Range("A2:H15"))
.Display
End With
cleanup:
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
'''''''''''''''''
Function RangetoHTML(rng As Range)
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
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