PDA

View Full Version : Attached sheet as Email Body



jammer6_9
01-18-2012, 01:12 AM
Dear experts,

Below codes works fine but into 2 separate procedure. Could you help me out combining it? Objective is to have a procedure which will create a Workbook summary file together with a snapshot of one of the sheet which will be attached on the email body.

Emailing Selected Sheets as attachment

Sub Mail_Sheets_Array()
On Error Resume Next

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set Sourcewb = ActiveWorkbook

'Copy the sheets to a new workbook
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Report Summary", "Inter_Department", "C-PettyCash", "C-Cashier", "C-INV", "C-ActionPlan", "Scoring_sheet")).Copy
End With

'Close temporary Window
TempWindow.Close

Set Destwb = ActiveWorkbook

'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else

If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With

'Change all cells in the worksheets to values if you want
For Each sh In Destwb.Worksheets
sh.Select
With sh.UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Destwb.Worksheets(1).Select
Next sh

'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Audit Report - " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With Destwb
.SaveAs TempFilePath & Sourcewb.Name & FileExtStr, FileFormat:=FileFormatNum
On Error Resume Next
With OutMail
.To = Range("cy3").Value
.cc = ""
.BCC = ""
.Subject = "Audit Report -"
.Attachments.Add Destwb.FullName

'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Display 'or use .Send
End With
On Error GoTo 0
.Close SaveChanges:=False
End With

'Delete the file you have send
Kill TempFilePath & Sourcewb.Name & FileExtStr

Set OutMail = Nothing
Set OutApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub



I want to combine below code to above procedure to get one of the sheet as email body


Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()

Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range

On Error GoTo StopMacro

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set Sendrng = Worksheets("Report Summary").Range("b2:aa40")

Set AWorksheet = ActiveSheet

With Sendrng

.Parent.Select

Set rng = ActiveCell

.Select

ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope

.Introduction = "Fine below summary of the audit."

With .Item
.To = ""
.Subject = ""
.Send
End With

End With

rng.Select
End With

AWorksheet.Select

StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False


End Sub

mdmackillop
01-18-2012, 02:31 PM
You can pass a specific range to the second sub as follows

in first Sub
Dim r As Range
Set r = Worksheets("Report Summary").Range("b2:aa40")
Call Send_Range_Or_Whole_Worksheet_with_MailEnvelope(r)


Amend second sub as follows

Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope(SendRng As Range)

Dim AWorksheet As Worksheet
Dim SendRng As Range
Dim rng As Range

On Error GoTo StopMacro

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Delete this line
'Set SendRng = Worksheets("Report Summary").Range("b2:aa40")
Set AWorksheet = ActiveSheet
With SendRng
'etc.