Hi all
I'm using a macro to compile an email. I have used this macro before on some other projects but now I need to make some adjustments and I'm struggling a little bit.
The main adjustments that I'm trying to achieve are:- Instead copying entire sheet and attach it to the email I only want to copy section of it (named range "Charts_breakdown").
- I would also like the macro to copy and rename a PDF file (Statistics.ppt - this file will be open before launching any macro). Macro should rename it as per value of "emailRenamePDFattachment", attach it to the email and kill the Statistics.ppt without saving it.
I have made some notes on the code. Example spreadsheet is attached. It has a macro button ("Compile EMAIL") to launch the current code. There is also a sheet called "Example of attached sheet" that is simulating the outcome of the sheet that should be attached to the email.
Global emailTempSaveFolder As String, emailSheetName As String, emailTo As String, emailCC As String, emailSubject As String, emailAttachedFile As String, emailTextBody As String, emailRenamePDFattachment As String
'---vvv-- This part of the code sole purpose is to picking up settings (values in cells) from sheet called "properties email". These will be later used to compile the email tha will later
Sub EmailVariables_1()
With ThisWorkbook.Sheets("properties email")
'---vvv-- Location on of a temporary folder to temporarily save the speadsheet
emailTempSaveFolder = .Range("B6")
'---vvv-- Name of the sheet that will be copied fully copied over (This is what I would like to change and make obsolete)
emailSheetName = .Range("C10")
emailTo = .Range("C11")
emailCC = .Range("C12")
emailSubject = .Range("C13")
emailAttachedFile = .Range("C14")
'---vvv-- Name of the sheet that will be copied (This is what I would like to only use to name the file that will be attached)
emailTextBody = .Range("C15") & vbNewLine & vbNewLine & _
.Range("C16") & vbNewLine & _
.Range("C17") & vbNewLine & _
.Range("C18") & vbNewLine & vbNewLine & _
.Range("C19") & vbNewLine & vbNewLine & _
.Range("C20") & vbNewLine & vbNewLine & vbNewLine & _
.Range("C21") & vbNewLine & _
.Range("C22")
emailRenamePDFattachment = .Range("C25")
End With
End Sub
Sub Email_launch_standard_email_1()
Call EmailVariables_1
Dim oApp As Object
Dim oMail As Object
Dim WB As Workbook
Dim FileName As String
Dim wSht As Worksheet
Dim shtName As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'---vvv-- from this point onwards I'm bit stuck. I would like to change but don't know how.
'At the moment it copies entire sheet and pastes it to a new file. This will include some charts etc.
Sheets(emailSheetName).Copy
Sheets(emailSheetName).UsedRange.Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'What I would like it to do instead is to copy named range "Charts_breakdown" (essentially cells A85 to V147),
'and paste it to the attached sheet cell 1,row 1 downwards. Then I would like the macro to delete row 2 (as it has some dummy data) and autofit all columns.
'--AAA---------------------------------------------------------
Set WB = ActiveWorkbook
FileName = emailAttachedFile & " " & Format(Now, "dd-mm-yyyy")
On Error Resume Next
Kill emailTempSaveFolder & FileName
On Error GoTo 0
WB.SaveAs FileName:=emailTempSaveFolder & " " & FileName
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = emailTo
.CC = emailCC
.Subject = emailSubject & " " & Format(Now, "dd-mm-yyyy")
.Body = emailTextBody
.Attachments.Add WB.FullName
.Display
End With
WB.ChangeFileAccess Mode:=xlReadOnly
Kill WB.FullName
WB.Close SaveChanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Set oMail = Nothing
Set oApp = Nothing
'---vvv-- From this point onwards I would like the macro to copy PDF file (Statistics.ppt - this file will be open before launching any macro)
'rename it as per value of "emailRenamePDFattachment", attach it to the email and kill the Statistics.ppt without saving it
End Sub
Any help would be extremely helpful.
Cheers
Bookmarks