VBA Macro used in the Microsoft Office Excel Application

Excel Create Emails with Workbooks Attached | VBA Macro #23

Share on facebook
Share on twitter
Share on linkedin
If you work will multiple clients you might be assigned with separating data in Excel and sending this information from Outlook.  To complete this task in Excel you could filter the data and move client specific information into separate Workbooks.  Then in Outlook create new emails, enter the appropriate recipients, add an email subject and type out a message in the email body.  Finally ensure you attach the correct Workbook to the appropriate email.  This process takes a lot of time and leaves a lot of room for error.  Avoid the potential to make a mistake and use VBA!  VBA gives you a way to automatically create Outlook emails out of Excel and add Workbooks as attachments.  This Excel macro will use the Folder Path indicated in the code to find potential email attachments.  By Default, the Folder Path will be the same as the Active Workbook.  Once run this macro will use the name of each Worksheet to find files with the same name.  If a match is found a new email with be generated with the Workbook attached.  The Outlook email generated will have the subject and body populated.  With a few updates to the VBA code you can have specific recipients pull in based on the Worksheet name. This Excel Create Emails with Workbooks Attached macro can be used as the final action after using Excel Create Worksheet per Filter Value and Excel Save Worksheets as Workbooks.  These macros used in this sequence provides an efficient way to communicate relevant information to multiple clients. You can automate an entire process when you combine macros together!

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results"
Sub CreateEmailswithWorkbooksAttached()
' Means variable is in use
Dim CCRecipients As String '
Dim FolderPath As String '
Dim i As Integer '
Dim objNewEmail As Object '
Dim objOutlookApp As Object '
Dim oFile As Object '
Dim oFolder As Object '
Dim oFSO As Object '
Dim Recipients As String '

On Error GoTo LeverageLean
 
FolderPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) - Len(ActiveWorkbook.Name)) 'Active Workbook Folder Path

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(FolderPath)

For i = 2 To Worksheets.Count 'Loop through every Worksheet (except the first one)
For Each oFile In oFolder.Files 'Loop through every File in FolderPath
If oFile.Name = Sheets(i).Name & Right(oFile.Name, (Len(oFile.Name) + 1) - InStrRev(oFile.Name, ".")) Then 'If the File Name equals the Worksheet Name

'Using the Worksheet Name indicate who the Email Recipients should be
If Sheets(i).Name = "Marketing" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "Sales" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "IT" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "HR" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "Operations" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "PR" Then
Recipients = ""
'CCRecipients = ""
ElseIf Sheets(i).Name = "Finance" Then
Recipients = ""
'CCRecipients = ""
End If

'Create a new email
Set objOutlookApp = CreateObject("Outlook.Application")
Set objNewEmail = objOutlookApp.CreateItem(olMailItem)

objNewEmail.To = Recipients
'objNewEmail.CC = CCRecipients 'Update to include CCed Recipients to emails
objNewEmail.Subject = Sheets(i).Name & ": Review attached Workbook"
objNewEmail.HTMLBody = "<HTML><BODY><span style=""font-family : Calibri;font-size : 11pt"">" & _
    "Hello " & Sheets(i).Name & ",<br></br><br></br>" & _
    "Can you please review the attached Workbook?  Please contact me with any questions or concerns.<br></br><br></br>" & _
    "Stay Awesome!</span></BODY></HTML>"
objNewEmail.Attachments.Add FolderPath & "\" & oFile.Name
objNewEmail.Display
'objNewEmail.Send 'Automatically send the email

End If
Next oFile

Next

Set objNewEmail = Nothing
Set objOutlookApp = Nothing
Set oFile = Nothing
Set oFolder = Nothing
Set oFSO = Nothing

Exit Sub

LeverageLean:
MsgBox (Err.Number & " - " & Err.Description & vbNewLine & vbNewLine & "Don't hesitate to email me: brentschneider@leveragelean.com")
  
End Sub
'Stay Awesome

Macro Monday

Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!

Customization

These lines of code can be customized to personalize this macro.
16. FolderPath = Left(ActiveWorkbook.FullName, Len(ActiveWorkbook.FullName) – Len(ActiveWorkbook.Name)) ‘Active Workbook Folder Path By Default the Folder Path is the Active Workbook but you can change this to a different path if desired
21. For i = 2 To Worksheets.Count ‘Loop through every Worksheet (except the first one) By Default this macro will start at the 2nd Worksheet and loop through all the Worksheets following it. You can update 2 to a 1 to include the 1st Worksheet.
25 through 47 ‘Using the Worksheet Name indicate who the Email Recipients should be Update the Sheet Names and set the Recipients (also CCRecipients) with the appropriate email addresses
54. ‘objNewEmail.CC = CCRecipients ‘Update to include CCed Recipients to emails If CC’ing Recipients then update this code line to not be a comment
55. objNewEmail.Subject = Sheets(i).Name & “: Review attached Workbook” This code line controls what populates in the Email Subject
56 through 59 objNewEmail.HTMLBody These code lines control what will populate into the Email Body
62. ‘objNewEmail.Send ‘Automatically send the email If you want the email to send automatically then update this code line to not be a comment
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Excel Macros

Recent Macros

VBA Macro used in the Microsoft Office Word Application

Word InputBox Call Macros | VBA Macro #27

More Macros More Buttons.  If you create enough macros you will begin to question if a button needs to be created for each one.  It is overwhelming when there are too many custom macro buttons to select from. This also makes navigating to run a specific macro difficult. To address this issue, we created the macro Word InputBox Call Macros! This Word macro displays an InputBox with a list of macros that can be called.  To call a specific macro just enter in the leading number and click ok. The concept of this macro is simple and helps you stay organized storing all macros within one button.

VBA Macro used in the Microsoft Office Excel Application

Excel Update Multiple Workbooks from One | VBA Macro #26

This Excel macro is the inverse of Copy Multiple Workbooks into One where separate Workbooks are being combined into one MASTER Workbook.  The Update Multiple Workbooks from One macro instead takes the current information from the MASTER Workbook and updates separate Workbooks. If you desire one source of information that can be easily distributed across multiple parties this macro is for you!

VBA Macro used in the Microsoft Office Outlook Application

Outlook Create Multiple Emails | VBA Macro #25

You just completed a task on your computer and now you need to email some impacted parties letting them know the task is complete.  Let’s say for example you have to email the client, the billing department, and your supervisor.  You could send one email including all these recipients but this is a problem if you are sending sensitive information specific to everyone involved.  Even if there isn’t sensitive information someone can use Reply All creating an email thread with everyone included. There has to be a better way…of course there is with VBA! This Outlook macro will create multiple emails based on the number that you indicate in an InputBox.  Think of this Create Multiple Emails macro as a code template that allows you generate multiple emails pertaining to different topics. Using the example above the first email can be dedicated to the client, the second email for the billing department, and the third email for your supervisor.

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Excel, Outlook, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Copyright © 2019 Leverage & Lean – Uptown Style WordPress theme by GoDaddy