VBA to split into separate spreadsheets and email with Outlook template

Holley

Board Regular
Joined
Dec 11, 2019
Messages
120
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all! I am curious if a task is possible with VBA. I work with a huge spreadsheet that I would like to automate. The data under each client will need to be copied and saved to a new worksheet and then we email out individual worksheets using a template. There is an Outlook contact named with the client #. Would it be possible to save each and search outlook for the contact to email the spreadsheet to?

In the spreadsheet example, I would need to end up with 4 spreadsheets. One for each client # listed and then email it to that contact.
1665779660094.png

I hope I am explaining this clearly.

I don't mind putting the work in to get this going, just not sure if it is even doable.

Thanks in advance!!!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It is much easier and more accurate to work with an example workbook instead of a picture.

Please provide an actual workbook with the data shown in your image. The workbook can be added to a 'cloud website' for download ... you will need to provide the download link
here.

Thank you.
 
Upvote 0
I hope I did this correctly...

test.xlsx
ABCDEFGHIJKLM
1
2TypeClient #Cust #Cust NameInv DateInv #Inv AmtDue DateDocument NeededRqst DatePast DueAge of Request
3Email12341234567ABC Store5/12/2022123$1.007/11/2022Invoice Copy9/30/20229610/1415
41234 Total$1.00
5Email45689876543DEF Store6/9/2022456$1.009/7/2022Invoice Copy & POD9/30/20223810/1415
6Email45689876543DEF Store6/9/2022789$1.009/7/2022Invoice Copy & POD9/30/20223810/1415
74568 Total$2.00
8Email09879876543DEF Store6/10/20221234$1.009/8/2022Invoice Copy & POD9/30/20223710/1415
90987 Total$1.00
10Email65439876543DEF Store6/10/20224568$1.009/8/2022Invoice Copy & POD9/30/20223710/1415
11Email65432101234GHI Inc6/9/20229101$1.007/9/2022Invoice Copy & POD7/21/20229810/1486
12Email65432101234GHI Inc7/26/20221234$1.008/25/2022Invoice Copy & POD8/25/20225110/1451
136543 Total$3.00
14Grand Total$7.00
Sheet1
Cell Formulas
RangeFormula
K3,K10:K12,K8,K5:K6K3=IF(L3,L3-H3,"")
L3,L10:L12,L8,L5:L6L3=NOW()
M3,M10:M12,M8,M5:M6M3=+L3-J3
G4,G9G4=SUBTOTAL(9,G3:G3)
G7G7=SUBTOTAL(9,G5:G6)
G13G13=SUBTOTAL(9,G10:G12)
G14G14=SUBTOTAL(9,G3:G12)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:M14Expression=$M3>=30textNO
 
Upvote 0
Ok ... good stuff to work with so far.

The people who will be receiving these emails ... Is it a huge job to manually enter their email address into this workbook ?
If so ... is their something unique in each of their emails that are sitting in Outlook that matches them ? Example: the Client Number ... is
that also contained in each of their Outlook emails ? If so, where is the client number located ? Is it in the body of the email ... is it in the
TO field along with their email address ? Going to need a sample email with this information or you can decide to manually enter the
email addresses.
 
Upvote 0
Ok ... good stuff to work with so far.

The people who will be receiving these emails ... Is it a huge job to manually enter their email address into this workbook ?
If so ... is their something unique in each of their emails that are sitting in Outlook that matches them ? Example: the Client Number ... is
that also contained in each of their Outlook emails ? If so, where is the client number located ? Is it in the body of the email ... is it in the
TO field along with their email address ? Going to need a sample email with this information or you can decide to manually enter the
email addresses.
Thanks!!

Yes, the client # is the name of the contact for example client 1234 is how the contact is saved, but it has the actual email address. Below is how the contacts are set up. When we send manually, we simply enter the client # in the to field and it populates the email address.

1665875110598.png


This is how the email goes out... The spreadsheet is named the client #.
1665875465226.png
 

Attachments

  • 1665875426052.png
    1665875426052.png
    12.2 KB · Views: 2
Upvote 0
This macro is supposed to create a list of your contacts contained in your copy of Outlook. I dont use Outlookso I am unable to test the macro here. Run it on your end and see
if it extracts all your email contacts. If it does, you will need to verify all the Client Emails are in the list. Then we can go from there.

VBA Code:
Option Explicit

Sub Import_Contacts()

    'Outlook objects.
    Dim olApp As Outlook.Application
    Dim olNamespace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olConItems As Outlook.Items
    Dim olItem As Object
    
    'Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    
    'Location in the imported contact list.
    Dim lnContactCount As Long
    
    Dim strDummy As String
    
    'Turn off screen updating.
    Application.ScreenUpdating = False
    
    'Initialize the Excel objects.
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets(1)
    
    'Format the target worksheet.
    With wsSheet
        .Range("A1").CurrentRegion.Clear
        .Cells(1, 1).Value = "Company / Private Person"
        .Cells(1, 2).Value = "Street Address"
        .Cells(1, 3).Value = "Postal Code"
        .Cells(1, 4).Value = "City"
        .Cells(1, 5).Value = "Contact Person"
        .Cells(1, 6).Value = "Email"
        With .Range("A1:F1")
            .Font.Bold = True
            .Font.ColorIndex = 10
            .Font.Size = 11
        End With
    End With
    
    wsSheet.Activate
    
On Error Resume Next
    'Initialize the Outlook variables with the MAPI namespace and the default Outlook folder of the current user.
    Set olApp = New Outlook.Application
    Set olNamespace = olApp.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(10)
    Set olConItems = olFolder.Items
            
    'Row number to place the new information on; starts at 2 to avoid overwriting the header
    lnContactCount = 2
    
    'For each contact: if it is a business contact, write out the business info in the Excel worksheet;
    'otherwise, write out the personal info.
    For Each olItem In olConItems
        If TypeName(olItem) = "ContactItem" Then
            With olItem
                If InStr(olItem.CompanyName, strDummy) > 0 Then
                    Cells(lnContactCount, 1).Value = .CompanyName
                    Cells(lnContactCount, 2).Value = .BusinessAddressStreet
                    Cells(lnContactCount, 3).Value = .BusinessAddressPostalCode
                    Cells(lnContactCount, 4).Value = .BusinessAddressCity
                    Cells(lnContactCount, 5).Value = .FullName
                    Cells(lnContactCount, 6).Value = .Email1Address
                Else
                    Cells(lnContactCount, 1) = .FullName
                    Cells(lnContactCount, 2) = .HomeAddressStreet
                    Cells(lnContactCount, 3) = .HomeAddressPostalCode
                    Cells(lnContactCount, 4) = .HomeAddressCity
                    Cells(lnContactCount, 5) = .FullName
                    Cells(lnContactCount, 6) = .Email1Address
                End If
                wsSheet.Hyperlinks.Add Anchor:=Cells(lnContactCount, 6), _
                                       Address:="mailto:" & Cells(lnContactCount, 6).Value, _
                                       TextToDisplay:=Cells(lnContactCount, 6).Value
            End With
            lnContactCount = lnContactCount + 1
        End If
    Next olItem
    
    'Null out the variables.
    Set olItem = Nothing
    Set olConItems = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
    
    'Sort the rows alphabetically using the CompanyName or FullName as appropriate, and then autofit.
    With wsSheet
        .Range("A2", Cells(2, 6).End(xlDown)).Sort key1:=Range("A2"), order1:=xlAscending
        .Range("A:F").EntireColumn.AutoFit
    End With
            
    'Turn screen updating back on.
    Application.ScreenUpdating = True

    MsgBox "The list has successfully been created!", vbInformation
    
End Sub

At a minimum you will need the following references activated :
 

Attachments

  • VBA References.jpg
    VBA References.jpg
    47.8 KB · Views: 4
Upvote 0
This macro is supposed to create a list of your contacts contained in your copy of Outlook. I dont use Outlookso I am unable to test the macro here. Run it on your end and see
if it extracts all your email contacts. If it does, you will need to verify all the Client Emails are in the list. Then we can go from there.

VBA Code:
Option Explicit

Sub Import_Contacts()

    'Outlook objects.
    Dim olApp As Outlook.Application
    Dim olNamespace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olConItems As Outlook.Items
    Dim olItem As Object
   
    'Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
   
    'Location in the imported contact list.
    Dim lnContactCount As Long
   
    Dim strDummy As String
   
    'Turn off screen updating.
    Application.ScreenUpdating = False
   
    'Initialize the Excel objects.
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets(1)
   
    'Format the target worksheet.
    With wsSheet
        .Range("A1").CurrentRegion.Clear
        .Cells(1, 1).Value = "Company / Private Person"
        .Cells(1, 2).Value = "Street Address"
        .Cells(1, 3).Value = "Postal Code"
        .Cells(1, 4).Value = "City"
        .Cells(1, 5).Value = "Contact Person"
        .Cells(1, 6).Value = "Email"
        With .Range("A1:F1")
            .Font.Bold = True
            .Font.ColorIndex = 10
            .Font.Size = 11
        End With
    End With
   
    wsSheet.Activate
   
On Error Resume Next
    'Initialize the Outlook variables with the MAPI namespace and the default Outlook folder of the current user.
    Set olApp = New Outlook.Application
    Set olNamespace = olApp.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(10)
    Set olConItems = olFolder.Items
           
    'Row number to place the new information on; starts at 2 to avoid overwriting the header
    lnContactCount = 2
   
    'For each contact: if it is a business contact, write out the business info in the Excel worksheet;
    'otherwise, write out the personal info.
    For Each olItem In olConItems
        If TypeName(olItem) = "ContactItem" Then
            With olItem
                If InStr(olItem.CompanyName, strDummy) > 0 Then
                    Cells(lnContactCount, 1).Value = .CompanyName
                    Cells(lnContactCount, 2).Value = .BusinessAddressStreet
                    Cells(lnContactCount, 3).Value = .BusinessAddressPostalCode
                    Cells(lnContactCount, 4).Value = .BusinessAddressCity
                    Cells(lnContactCount, 5).Value = .FullName
                    Cells(lnContactCount, 6).Value = .Email1Address
                Else
                    Cells(lnContactCount, 1) = .FullName
                    Cells(lnContactCount, 2) = .HomeAddressStreet
                    Cells(lnContactCount, 3) = .HomeAddressPostalCode
                    Cells(lnContactCount, 4) = .HomeAddressCity
                    Cells(lnContactCount, 5) = .FullName
                    Cells(lnContactCount, 6) = .Email1Address
                End If
                wsSheet.Hyperlinks.Add Anchor:=Cells(lnContactCount, 6), _
                                       Address:="mailto:" & Cells(lnContactCount, 6).Value, _
                                       TextToDisplay:=Cells(lnContactCount, 6).Value
            End With
            lnContactCount = lnContactCount + 1
        End If
    Next olItem
   
    'Null out the variables.
    Set olItem = Nothing
    Set olConItems = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
   
    'Sort the rows alphabetically using the CompanyName or FullName as appropriate, and then autofit.
    With wsSheet
        .Range("A2", Cells(2, 6).End(xlDown)).Sort key1:=Range("A2"), order1:=xlAscending
        .Range("A:F").EntireColumn.AutoFit
    End With
           
    'Turn screen updating back on.
    Application.ScreenUpdating = True

    MsgBox "The list has successfully been created!", vbInformation
   
End Sub

At a minimum you will need the following references activated :
worked perfectly!
 
Upvote 0
Post a copy of the final result so it can be downloaded. This website doesn't have a means of posting a workbook ... you will need to use a 3rd party cloud website like DropBox.com or something similar. Once I can download it, it will be possible for me to work on the remainder of the code.
 
Upvote 0
Post a copy of the final result so it can be downloaded. This website doesn't have a means of posting a workbook ... you will need to use a 3rd party cloud website like DropBox.com or something similar. Once I can download it, it will be possible for me to work on the remainder of the code.
test2.xlsx
ABCDEF
1Company / Private PersonStreet AddressPostal CodeCityContact PersonEmail
212341234holley@ab.com
345684568holley@cd.com
4987987holley@ef.com
565436543holley@gh.com
Sheet1
 
Upvote 0
There are only 4 companies in total ? I mistakenly believed there would be hundreds ?

???
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top