|
Email Current Workbook & or Other Attachments
This macro will send the current workbook in an email through Microsoft Outlook. The Macro allows you to send the most recently saved version of the excel file you are working in as an attachment. You must use the email client Outlook and you will need to have an internet connection. The specifics of the email are all contained within the macro. This means that you will have to edit certain portions of the macro in order to send an email to the desired recipients and also with the desired message.
NOTE
In the macro code you should notice a series of lines which start at the line ".To" then ".CC" etc. This text reads exactly like it does in an email and you should fill in the space where it says ADD TEXT HERE with exactly what you would put in these areas of a regular email.
Where it reads ".To" and then says JOHNDOE@TEST.COM, replace this sample email with the email address or addresses where you would like this email to be sent.
Also, directly underneath the line which reads " .Attachments.Add ActiveWorkbook.FullName", you can enter additional attachments to add to the email. Sample code for this is " .Attachments.Add ("C:\myfiles\another_file.xls")" This means that another file on your computer located in the folder C:\myfiles\ will be sent with this email as an attachment; the file's name (including file extension [.xls]) is "another_file.xls"
Where to install the macro: Module
Email Current Workbook & or Other Attachments
Sub Send_Email_Current_Workbook()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "JOHNDOE@TEST.COM"
.CC = "ADD TEXT HERE"
.BCC = "ADD TEXT HERE"
.Subject = "ADD TEXT HERE - Subject"
.Body = "ADD TEXT HERE"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
Hi,
I am looking for a macro that will copy the current range into an email, with the email address being stored in cell A1, and the range being cells that are autofiltered by company.
Basically want it so that I can select e.g. Company 4 on the autofilter, and the filtered range will be put into a draft mail with the title "Bonds" and the email address being dragged from cell A1...
Shouldn't be too hard?
Regards
Luke
I have a Workbook that runs automatically and emails itself to a distribution list. What I'd like to do is make it so that the Workbook saves a copy of itself, in a .xlsx format (no macros) and THEN distribute.
How would I do this without changing the actual workbook that I'm currently in (using SaveAs).
Hello everyone I need some help. When I right click on an excel attachment and click save as I always have to type in the extension even though my file extensions are hidden. The save as type: is set to All files. The drop down menu does not have excel. If I open the file and click save as it automatically saves as an excel docment. Please help me correct this. I just formatted my computer and this issue was not present before the cleaning.
Thanks in advance.
Hello Everybody,
Does anybody know if it is possible excel (VBA) to open an excel attachment in outlook?
I have an excel report that gets its data from email attachments and I'm currently opening the attachment and pasting the data into my report (got to be a better way). What I would like my report to do is;
1) Search for the attachment
2) Save the attachment to a temp file
From the temp file I know enough to transfer the data to my report.
Any suggestions would be greatly appreciated
A Meynell
Hi, i receive emails daily from 30 or 40 people with the same attachment. Is there any way i can look at consolidating all these?
When I attempt to use the Send To Mail Recipient (as Attachment) function in
Excel, some spreadsheet files are sent broken up into as many as 30 separate
e-mails. Some people receive all 30 emails and Outlook Express consolidates
them into one. But, others receive 30 emails of gibberish. Why is this
happening? What can I do on my end to fix this issue?
Hi,
I have been using some CDO email code for a while now, but I have only ever needed to attach 1 file.
For my current project I need to send upto 3 attachments.
The first depends on if there is a file path in name range "PDESC" and the other 2 files are the activeworkbook, which is saved in a folder called STAFF CHANGES BACKUP on the desktop, along with a word doc with the same file name as the active workbook found in the same folder.
So there should always be 2 attachments but occasionally there should be 3. Another problem I noticed when just testing the attachment who's file path is in range PDESC is that if the field is blank it creates a blank attachment which I don't really want.
Any ideas?
Here is my email code:
Code:
Public xBook As Workbook
Public iSh As Worksheet
Sub EMAILLIST()
Dim cell As Object
Dim NR As Long
Dim tagerror As String
Dim Email_Send_To, Email_Send_From, Email_Subject, Email_Body As String
Dim strUserEmail As String
Dim strFirstClassPassword As String
Dim errPar As String
Dim iMsg As Object
Dim iConfig As Object
Dim sConfig As Variant
Dim Row As Integer
Dim Atc As String
Set xBook = activeworkbook
Set iSh = xBook.Sheets("INPUT")
strUserEmail = xBook.Sheets("INPUT").Range("MMAIL")
strFirstClassPassword = ""
Set iMsg = CreateObject("CDO.Message")
Set iConfig = CreateObject("CDO.Configuration")
iConfig.Load -1
Set sConfig = iConfig.Fields
With sConfig
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.168.0.5" 'Name or IP of remote SMTP server
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'Server Port
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = strUserEmail
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = strFirstClassPassword
.Update
End With
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'-----------------------------------------------------------------------------
Email_Send_To = iSh.Range("AMAIL")
Email_Send_From = iSh.Range("MMAIL")
Email_Subject = "CHANGE REQUEST"
Email_Body = "Dear " & iSh.Range("ANAME") & "," & vbNewLine & vbNewLine _
& "Please complete this change request which was submitted by " & iSh.Range("MNAME") _
& " on the " & Format(Now, "dd/mm/yyyy") & ". To complete this request please download the " _
& "attached files to your desktop. If you only have a word document, please print this off, " _
& "sign it and return it to HR. If there is also an excel sheet, once you have downloaded it " _
& "open it and check the details. If you give permission for this change request, click the " _
& "'Authorise' button. If you get a message saying unable to connect, then you must print off " _
& "the word document and submit it to HR." & vbNewLine & vbNewLine _
& "Any queries should be directed to your segment's HR team who will be happy to help." _
& vbNewLine & vbNewLine _
& "Kind regards," & vbNewLine & vbNewLine _
& "HR"
'-----------------------------------------------------------------------------
With iMsg
Set .Configuration = iConfig
End With
iMsg.To = Email_Send_To
iMsg.From = Email_Send_From
iMsg.Subject = Email_Subject
iMsg.Textbody = Email_Body
iMsg.AddAttachment iSh.Range("PDESC")
iMsg.Send
Exit Sub
On Error GoTo tagerror
'clean_up:
' With Application
' .EnableEvents = True
' .ScreenUpdating = True
' End With
tagerror:
MsgBox "Error: (" & Err.Number & ") " & Err.Description & " at " & Err.Source, vbCritical
'Resume clean_up
End Sub
I have a worksheet, which has the following:
1) Coloum A have the name of the files of the attachments;
2) Coloum B have the email addresses;
3) Coloum C have the path where the attachement are saved;
I want a macro which can email all the attachements with the name as in Coloum A to there corresponding email address as mentioned in Coloum B. All attachments are saved in the same folder, path is mentioned in Coloum C.
I want these mails to be displayed and not sent directly.
All excel/macro gurus, please help.
Hi there,
I am writing a routine to attach a zip file to an email, add a delivery reciept to this email, then send it.
The routine I have here needs to be updated so that it attches a file to the email AND to have the delivery reciept added. Can someone show me how please?
Code:
Sub OM_EmailToAviva()
' =============================================================================================
' This macro is the routine that is ran by the Fulfilment team on a daily basis.
' =============================================================================================
Dim AvivaAddress As String
Dim OutApp, OutMail As Object
Dim Sh As Worksheet
Application.DisplayAlerts = False
Application.ScreenUpdating = False
AvivaAddress = "XXXXX"
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "" & AvivaAddress & ""
.Subject = "OM Upload File: " & Format(Now, "dd-mmmm-yyyy")
' =============================================================================================
' These two options can be alternated between depending on the requirements.
.Display
' .Send
' =============================================================================================
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Good morning all!
I have code that works just fine. I now need to add two different attachments and when I try i get an error that says cant find path. By themselves they both can be found. anyone have any ideas?
Code:
Sub emailtest()
Dim TodayFile
Dim FileDate
Dim attachmnt2
Dim attachmnt
'Sends a specified range to a Outlook message and retains Excel formatting
'References needed :
'Microsoft Outlook Object Library := msoutl9olb
'Microsoft Scripting Runtime := SCRRUN.DLL
'Dimension variables
'// ie. if Ol not installed then we need to Late bind & define as Obj
'// Use Late binding > Outlook.Application > olMail As Outlook.MailItem
'// Plus define olmailItem
Dim olApp As Object, olMail, olMailItem
Dim FSObj As Scripting.FileSystemObject, TStream As Scripting.TextStream
Dim rngeSend As Range, strHTMLBody As String
'On Error Resume Next
'Select the range to be sent
Set rngeSend = Range("B2:J32")
'Now create the HTML file
'// Changes by IFM
'// changed "C:\temp\sht.htm" to Dynamically get Sys Temp Dir
'// To cover the instance where Tmp Dir is NOT @ C:\
Dim SysTmp As String
SysTmp = TmpFolderLocation
ThisWorkbook.Sheets("sumpastetab").Range("A1").Select
ActiveWorkbook.PublishObjects.Add(xlSourceRange, SysTmp & "\sht.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(SysTmp & "\sht.htm", ForReading)
'Now set the HTMLBody property of the message to the text contained in the TextStream object
strHTMLBody = TStream.ReadAll
Worksheets("SumPasteTab").Activate
TodayFile = Range("Q27").Value
FileDate = Range("N2").Value
attachmnt = "S:\USA-HOUSTON\CPDS\GT FAMM\IctsOnline\Global Trading\Risk Control Daily Management Report\" & TodayFile & ".pdf"
attachmnt2 = "S:\USA-HOUSTON\CPDS\GT FAMM\IctsOnline\Global Trading\Risk Control Daily Management Report\" & "RMA GS&T Template-NEW BOOK STRUCTURE_TEST.pdf"
With olMail
.To = "hlcu@chevron.com"
.Subject = " Global DPR " & Format(Date, "mm-dd-yyyy")
'.Body = str & Application.Rept(Chr(13), 5) & str1 & Application.Rept(Chr(13), 5) & str2
.HTMLBody = strHTMLBody
.Attachments.Add (attachmnt) & (attachmnt2)
.Display
'.Send
End With
End Sub
|
|