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 Outlook Application

Outlook Greeting and Goodbye Contacts | VBA Macro #35

The Greeting and Goodbye Contact macro automates the entry of a Greeting “Good Morning” or “Hello” followed by the First Name of the recipient you are emailing. To identify the First Name this macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. The appropriate Greeting and Goodbye is determined by the current time and the day of the week. The Greeting and Goodbye Contacts macro helps you deliver better customer service and create better connections with your clients!

VBA Macro used in the Microsoft Office Excel Application

Excel Custom Questionnaire to File | VBA Macro #34

This is an Excel macro that generates a custom questionnaire to complete.  The Role field is used to determine what questions display for specific users.  Once a user selects their Role, they can click Generate to see what questions they need to complete to finish the questionnaire.  After a user has completed all the required questions, they can click Finish to create a separate Excel Workbook with all the populated answers.   You can customize where this new file is saved making it easy to know when someone has completed a questionnaire and is awaiting review. There is also the option to send this separate Excel Workbook in an Outlook email with a couple updates to the VBA code. There is always a better way with VBA and this Excel macro helps standardize the collection of answers to specific questions. This can help lead to a more formal review process and eliminate mistakes.

VBA Macro used in the Microsoft Office Outlook Application

Outlook Hello Contacts | VBA Macro #33

Starting an email with a Hello can look more personable with some VBA code! The Hello Contacts Outlook macro automates the entry of the text Hello followed by the First Name of the recipient you are emailing. This Outlook macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. This macro can be fired from a custom button or trigger automatically when replying to an email.

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