VBA Code for Automatic Email Draft Creation in Outlook 2010

sudarshank2

New Member
Joined
Nov 1, 2015
Messages
12
Hi Guys,

I have a path (Folder) and it contains the multiple excel files (say abc.xls , asd.xls and so on)

Once I run the macro if folder "contains" abc file name then it has to auto generate the email draft with the respective email id.

In sheet 2 I have mentioned abc and it's respective "TO", and "CC".

Note: If there are two file like abc.xls and abcde.xls then those file need to be attached in a single email draft.

Just let me know is it possible?

Thanks,
Sudarshan
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi

Yes, it’s possible.
I started working on your request, but have to go offline now.
Will return tomorrow…
 
Upvote 0
Today I’m on a machine without Outlook, so here is what the code does so far; I’ll complete it later:

- The sub to be executed is named Main
- Lets the user pick a directory
- Lists all files in it
- Groups the Excel files by name, allowing for multiple attachments on one message
- Displays message boxes on the content of each draft message
- Sheet2 should look like below

Sheet2

*ABC
1filetocc
2abc.xlsmbgates@microsoft.combjelen@mrexcel.com
3abcde.xlsmapope@chart.co.ukjpeltier@charting.com
4asd.xlslordvader@disney.comcmd_spock@starfleet.com
5example.xlsxpeterss@gdaymate.co.aulhamilton@mercedes.com

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Code:
Option Explicit


'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long


Declare Function SHBrowseForFolder Lib "shell32.dll" _
  Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
  
Public Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type


Sub Main()
Dim i%, av, lr%, r As Range, j%, hr%, sh As Worksheet, oln%, ns$, tbsh As Worksheet
Set sh = Sheets("List1")                        ' where file list is created
Set tbsh = Sheets("Sheet2")
ListFiles
ZtoA                                            ' order file list
oln = 1
Do
    ns = "Files attached:" & vbLf
    lr = sh.Range("a" & Rows.Count).End(xlUp).Row
    If InStr(sh.Cells(lr, 1), "xls") > 0 Then
        av = Split(sh.Cells(lr, 1).Value, ".")
        sh.Range("f" & lr + 2) = sh.Range("a1")
        sh.Range("f" & lr + 3) = av(0) & "*"
        sh.Range("a1:a" & lr).AdvancedFilter _
        xlFilterCopy, sh.Range("f" & lr + 2 & ":f" & lr + 3), sh.Range("h" & lr + 2), 0
        Set r = tbsh.Range("a:a").Find(sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8), tbsh.Cells(1, 1), xlValues)
        If Not r Is Nothing Then
            For j = sh.Range("h" & Rows.Count).End(xlUp).Row To lr + 3 Step -1
                ns = ns & sh.Cells(j, 8) & vbLf
            Next
            MsgBox ns & "To: " & r.Offset(, 1) & vbLf & "Cc: " & r.Offset(, 2), 64, "Email#" & oln
            oln = oln + 1
        Else
            MsgBox "Not found on Sheet2", vbCritical, sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8)
        End If
        Do Until sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8) = Range("a1") Or hr = 1
            hr = sh.Range("h" & Rows.Count).End(xlUp).Row
            Set r = sh.Range("a:a").Find(sh.Cells(hr, 8), Cells(1, 1), xlValues)
            If Not r Is Nothing Then r.EntireRow.Delete
            sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8).ClearContents
        Loop
    Else
        sh.Cells(lr, 1).EntireRow.Delete
    End If
Loop Until sh.Range("a2") = ""
End Sub


Sub ZtoA()
Dim lr%, ws As Worksheet
Set ws = Sheets("List1")
lr = ws.Range("a" & Rows.Count).End(xlUp).Row
Range("A2:A" & lr).Select
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("A2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=0
With ws.Sort
    .SetRange Range("a2:a" & lr)
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub
 
Sub ListFiles()
Dim Msg As String, Directory As String, f As String, r&
Msg = "Select a location containing the files you want to list."
Directory = GetDirectory(Msg)
If Directory = "" Then Exit Sub
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
r = 1
'   Insert headers
Cells.ClearContents
Cells(r, 1) = Directory
Cells(r, 2) = "Size"
Cells(r, 3) = "Date/Time"
Range("A1:C1").Font.Bold = True
'   Get first file
f = Dir(Directory, vbReadOnly + vbHidden + vbSystem)
Do While f <> ""
    r = r + 1
    Cells(r, 1) = f
    Cells(r, 2) = FileLen(Directory & f)
    Cells(r, 3) = FileDateTime(Directory & f)
'   Get next file
    f = Dir
Loop
End Sub


Function GetDirectory(Optional Msg) As String
    Dim bInfo As BROWSEINFO, path$
    Dim r As Long, x As Long, pos%
' Root folder = Desktop
    bInfo.pidlRoot = 0&
' Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
  End If
' Type of directory to return
    bInfo.ulFlags = &H1
' Display the dialog
    x = SHBrowseForFolder(bInfo)
' Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
  End If
End Function
 
Last edited:
Upvote 0
The project is now complete; emails are generated with single and multiple attachments:

Code:
Option Explicit


'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
  Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long


Declare Function SHBrowseForFolder Lib "shell32.dll" _
  Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long
  
Public Type BROWSEINFO
  hOwner As Long
  pidlRoot As Long
  pszDisplayName As String
  lpszTitle As String
  ulFlags As Long
  lpfn As Long
  lParam As Long
  iImage As Long
End Type
Dim otl As Object


Sub Main()
Dim i%, av, lr%, r As Range, j%, hr%, sh As Worksheet, oln%, ns$, tbsh As Worksheet
Set sh = Sheets("List1")                        ' where file list is created
Set tbsh = Sheets("Sheet2")
ListFiles
ZtoA                                            ' order file list
oln = 1
Set otl = CreateObject("Outlook.Application")
Do
    ns = ""
    lr = sh.Range("a" & Rows.Count).End(xlUp).Row
    If InStr(sh.Cells(lr, 1), "xls") > 0 Then
        av = Split(sh.Cells(lr, 1).Value, ".")
        sh.Range("f" & lr + 2) = sh.Range("a1")
        sh.Range("f" & lr + 3) = av(0) & "*"
        sh.Range("a1:a" & lr).AdvancedFilter _
        xlFilterCopy, sh.Range("f" & lr + 2 & ":f" & lr + 3), sh.Range("h" & lr + 2), 0
        Set r = tbsh.Range("a:a").Find(sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8), tbsh.Cells(1, 1), xlValues)
        If Not r Is Nothing Then
            For j = sh.Range("h" & Rows.Count).End(xlUp).Row To lr + 3 Step -1
                ns = ns & sh.Cells(1, 1) & sh.Cells(j, 8) & vbLf
            Next
            SaveAsDraft r.Offset(, 1), r.Offset(, 2), "see attached file(s).", oln, ns
            oln = oln + 1
        Else
            MsgBox "Not found on Sheet2", vbCritical, sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8)
        End If
        Do Until sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8) = Range("a1") Or hr = 1
            hr = sh.Range("h" & Rows.Count).End(xlUp).Row
            Set r = sh.Range("a:a").Find(sh.Cells(hr, 8), Cells(1, 1), xlValues)
            If Not r Is Nothing Then r.EntireRow.Delete
            sh.Cells(sh.Range("h" & Rows.Count).End(xlUp).Row, 8).ClearContents
        Loop
    Else
        sh.Cells(lr, 1).EntireRow.Delete
    End If
Loop Until sh.Range("a2") = ""
End Sub


Sub SaveAsDraft(sendto$, sendcc, ebody, oln%, ns$)
Dim mlmsg As Outlook.MailItem, f, i%
f = Split(ns, vbLf)
Set mlmsg = otl.CreateItem(0)
With mlmsg
    .To = sendto
    .cc = sendcc
    .Body = ebody
    .Subject = "Message #" & oln
    .Display
    For i = LBound(f) To UBound(f)
        If Len(f(i)) > 0 Then .Attachments.Add f(i)
    Next
    .Save
'   .Close
End With
End Sub


Sub ZtoA()
Dim lr%, ws As Worksheet
Set ws = Sheets("List1")
lr = ws.Range("a" & Rows.Count).End(xlUp).Row
Range("A2:A" & lr).Select
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("A2"), SortOn:=0, Order:=xlDescending, DataOption:=0
With ws.Sort
    .SetRange Range("a2:a" & lr)
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = 1
    .Apply
End With
End Sub
 
Sub ListFiles()
Dim Msg As String, Directory As String, f As String, r&
Msg = "Select a location containing the files you want to list."
Directory = GetDirectory(Msg)
If Directory = "" Then Exit Sub
If Right(Directory, 1) <> "\" Then Directory = Directory & "\"
r = 1
Sheets("List1").Activate
Cells.ClearContents
Cells(r, 1) = Directory
Cells(r, 2) = "Size"
Cells(r, 3) = "Date/Time"
Range("A1:C1").Font.Bold = True
'   Get first file
f = Dir(Directory, vbReadOnly + vbHidden + vbSystem)
Do While f <> ""
    r = r + 1
    Cells(r, 1) = f
    Cells(r, 2) = FileLen(Directory & f)
    Cells(r, 3) = FileDateTime(Directory & f)
'   Get next file
    f = Dir
Loop
End Sub


Function GetDirectory(Optional Msg) As String
Dim bInfo As BROWSEINFO, path$, r As Long, x&, pos%
' Root folder = Desktop
    bInfo.pidlRoot = 0&
' Title in the dialog
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
    Else
        bInfo.lpszTitle = Msg
  End If
' Type of directory to return
    bInfo.ulFlags = &H1
' Display the dialog
    x = SHBrowseForFolder(bInfo)
' Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal x, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetDirectory = Left(path, pos - 1)
    Else
        GetDirectory = ""
  End If
End Function
 
Upvote 0
Hi

I’m on another machine today, but we don´t need that test workbook of mine.
Create a new one and paste the code at post #4 on a standard module. After that, set up two sheets named Sheet2 and List1 with the appropriate data, as shown at post #3.
If this fails, tell me and I’ll prepare another workbook and send it to you.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

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