PDA

View Full Version : Attach ActiveSheet to Email



Emoncada
07-03-2007, 07:58 AM
I have this code that put's activesheet into the body of the email. I would now like to have it as an attachement. How Can I make that work.
[Sub Mail_Sheet_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With

Set rng = Nothing
Set rng = ActiveSheet.UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "Jane.Doe@yahoo.com"
.CC = ""
.BCC = ""
.Subject = "Phone Inventory"
.HTMLBody = RangetoHTML(rng)
'.Attachments.Add ("ActiveSheet")
.Send 'or use .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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook

TempFile = Environ$("temp") & "/" & Format(Now, "mm-dd-yy") & ".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

lucas
07-03-2007, 08:07 AM
Sub emailToSteve()
Application.Dialogs(xlDialogSendMail).Show arg1:="joe@aol.com"
End Sub

Emoncada
07-03-2007, 08:11 AM
That works but I don't want to send the entire spreadsheet just the active worksheet. Can that be done?

lucas
07-03-2007, 08:21 AM
You should search the kb and the forum before asking questions like this. You can do it as easily as I can do it for you:
http://vbaexpress.com/kb/getarticle.php?kb_id=97
http://vbaexpress.com/kb/getarticle.php?kb_id=326
http://vbaexpress.com/kb/getarticle.php?kb_id=350

Norie
07-03-2007, 09:58 AM
http://www.rondebruin.nl/sendmail.htm

johnske
07-06-2007, 12:36 AM
(Uses your default email program)

Sub EmailActiveSheetNoMessage()

Dim ThisDate As String, Recipient(1 To 10) As String, N As Long

ThisDate = Format(Date, "dd mmm yy")
For N = 1 To 10
'put your addies in Sheet1, A1:A10
Recipient(N) = Sheet1.Range("A" & N)
Next

Application.ScreenUpdating = False

ActiveSheet.Copy
With Cells
'get rid of formulas
.Copy
.PasteSpecial Paste:=xlPasteValues
End With

With ActiveWorkbook
.SendMail Recipient, "Files for " & ThisDate
.Saved = True
.Close False
End With
Application.ScreenUpdating = True

'Let user know what's happened
MsgBox "File sent by email ", , "Emailed..."
Sheet1.Activate

End SubIf you want to include a form message use


Option Explicit

Sub EmailActiveSheetWithMessage()

Dim ThisDate As String, Recipient(1 To 10) As String, N As Long

ThisDate = Format(Date, "dd mmm yy")
For N = 1 To 10
'put your addies in Sheet1, A1:A10
Recipient(N) = Sheet1.Range("A" & N)
Next

Application.ScreenUpdating = False

ActiveSheet.Copy
With Cells
'get rid of formulas
.Copy
.PasteSpecial Paste:=xlPasteValues
End With

'Send the new workbook with a message
ActiveWorkbook.HasRoutingSlip = True
With ActiveWorkbook.RoutingSlip
.Recipients = Recipient()
.Subject = "Files for " & ThisDate
.Message = "Hi," & vbNewLine & _
"" & vbNewLine & _
"Attached files are for...blah blah..." & vbNewLine & _
"...more blah here.... " & vbNewLine & _
"" & vbNewLine & _
"Regards," & vbNewLine & _
"Your name" & vbNewLine & _
"" & vbNewLine & _
"" & vbNewLine & _
"" & vbNewLine & _
""
.Delivery = xlAllAtOnce
.ReturnWhenDone = False
End With

With ActiveWorkbook
.Route
.Saved = True
.Close False
End With
Application.ScreenUpdating = True

'Let user know what's happened
MsgBox "File sent by email ", , "Emailed..."
Sheet1.Activate

End Sub