[FONT="]Can someone help me to combine mail merge attachment VBA code with looping folder and sub-folders code? I've tried out few options and its either shows error by sending emails without the attachments or doesn't finds attachment due to the location in sub-folders. [/FONT]
[FONT="]**My mail merge code is here:**
[/FONT][FONT="] Sub EmailTheFile()[/FONT]
[FONT="] Dim obMail As Outlook.MailItem[/FONT]
[FONT="] Dim irow As Integer[/FONT]
[FONT="] Dim pfile As String[/FONT]
[FONT="] Set fso = CreateObject("Scripting.FileSystemObject")[/FONT]
[FONT="] Set Folder = fso.GetFolder("C:\Users\VBA_Testing_Folder")[/FONT]
[FONT="] Set obMail = Outlook.CreateItem(olMailItem)[/FONT]
[FONT="] 'Loop through all files and send mail[/FONT]
[FONT="] irow = 26[/FONT]
[FONT="] Sheets("AR Form").Select[/FONT]
[FONT="] Range("A1:H62").Select[/FONT]
[FONT="] 'Creating an email[/FONT]
[FONT="] With obMail[/FONT]
[FONT="] .To = Range("B18").Value[/FONT]
[FONT="] .Subject = "Outstanding Balance"[/FONT]
[FONT="] .HTMLBody = "Testing" 'RangetoHTML(rng)[/FONT]
[FONT="] [/FONT]
[FONT="] 'looping and attaching the File listed in cell A26 and onwards[/FONT]
[FONT="] Do While Cells(irow, 1) <> Empty[/FONT]
[FONT="] [/FONT]
[FONT="] pfile = Dir(Folder & "" & Cells(irow, 1) & "*")[/FONT]
[FONT="] [/FONT]
[FONT="] If pfile <> "" And Right(pfile, 3) = "pdf" Then[/FONT]
[FONT="] .Attachments.Add (Folder & "" & pfile)[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="] If pfile <> "" And Right(pfile, 3) = "xls" Then[/FONT]
[FONT="] .Attachments.Add (Folder & "" & pfile)[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="] irow = irow + 1[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] .Display[/FONT]
[FONT="] .Send[/FONT]
[FONT="] End With[/FONT]
[FONT="] End Sub
[/FONT][FONT="]**Now I want to combine above code with TraversePath function**[/FONT]
[FONT="]---------------------------------------------------------------[/FONT]
[FONT="] Sub TraversePath12(path As String)[/FONT]
[FONT="] Dim currentPath As String, directory As Variant[/FONT]
[FONT="] Dim dirCollection As Collection[/FONT]
[FONT="] Set dirCollection = New Collection[/FONT]
[FONT="] [/FONT]
[FONT="] currentPath = Dir(path, vbDirectory)[/FONT]
[FONT="] [/FONT]
[FONT="] 'Explore current directory[/FONT]
[FONT="] Do Until currentPath = vbNullString[/FONT]
[FONT="] Debug.Print currentPath[/FONT]
[FONT="] If Left(currentPath, 1) <> "." And _[/FONT]
[FONT="] (GetAttr(path & currentPath) And vbDirectory) = vbDirectory Then[/FONT]
[FONT="] dirCollection.Add currentPath[/FONT]
[FONT="] End If[/FONT]
[FONT="] currentPath = Dir()[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] 'Explore subsequent directories[/FONT]
[FONT="] For Each directory In dirCollection[/FONT]
[FONT="] Debug.Print "---SubDirectory: " & directory & "---"[/FONT]
[FONT="] TraversePath path & directory & ""[/FONT]
[FONT="] Next directory[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] Sub Test12()[/FONT]
[FONT="] TraversePath12 "C:\Users\VBA_Testing_Folder"[/FONT]
[FONT="] End Sub[/FONT]
[FONT="]**My mail merge code is here:**
[/FONT][FONT="] Sub EmailTheFile()[/FONT]
[FONT="] Dim obMail As Outlook.MailItem[/FONT]
[FONT="] Dim irow As Integer[/FONT]
[FONT="] Dim pfile As String[/FONT]
[FONT="] Set fso = CreateObject("Scripting.FileSystemObject")[/FONT]
[FONT="] Set Folder = fso.GetFolder("C:\Users\VBA_Testing_Folder")[/FONT]
[FONT="] Set obMail = Outlook.CreateItem(olMailItem)[/FONT]
[FONT="] 'Loop through all files and send mail[/FONT]
[FONT="] irow = 26[/FONT]
[FONT="] Sheets("AR Form").Select[/FONT]
[FONT="] Range("A1:H62").Select[/FONT]
[FONT="] 'Creating an email[/FONT]
[FONT="] With obMail[/FONT]
[FONT="] .To = Range("B18").Value[/FONT]
[FONT="] .Subject = "Outstanding Balance"[/FONT]
[FONT="] .HTMLBody = "Testing" 'RangetoHTML(rng)[/FONT]
[FONT="] [/FONT]
[FONT="] 'looping and attaching the File listed in cell A26 and onwards[/FONT]
[FONT="] Do While Cells(irow, 1) <> Empty[/FONT]
[FONT="] [/FONT]
[FONT="] pfile = Dir(Folder & "" & Cells(irow, 1) & "*")[/FONT]
[FONT="] [/FONT]
[FONT="] If pfile <> "" And Right(pfile, 3) = "pdf" Then[/FONT]
[FONT="] .Attachments.Add (Folder & "" & pfile)[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="] If pfile <> "" And Right(pfile, 3) = "xls" Then[/FONT]
[FONT="] .Attachments.Add (Folder & "" & pfile)[/FONT]
[FONT="] End If[/FONT]
[FONT="] [/FONT]
[FONT="] irow = irow + 1[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] .Display[/FONT]
[FONT="] .Send[/FONT]
[FONT="] End With[/FONT]
[FONT="] End Sub
[/FONT][FONT="]**Now I want to combine above code with TraversePath function**[/FONT]
[FONT="]---------------------------------------------------------------[/FONT]
[FONT="] Sub TraversePath12(path As String)[/FONT]
[FONT="] Dim currentPath As String, directory As Variant[/FONT]
[FONT="] Dim dirCollection As Collection[/FONT]
[FONT="] Set dirCollection = New Collection[/FONT]
[FONT="] [/FONT]
[FONT="] currentPath = Dir(path, vbDirectory)[/FONT]
[FONT="] [/FONT]
[FONT="] 'Explore current directory[/FONT]
[FONT="] Do Until currentPath = vbNullString[/FONT]
[FONT="] Debug.Print currentPath[/FONT]
[FONT="] If Left(currentPath, 1) <> "." And _[/FONT]
[FONT="] (GetAttr(path & currentPath) And vbDirectory) = vbDirectory Then[/FONT]
[FONT="] dirCollection.Add currentPath[/FONT]
[FONT="] End If[/FONT]
[FONT="] currentPath = Dir()[/FONT]
[FONT="] Loop[/FONT]
[FONT="] [/FONT]
[FONT="] 'Explore subsequent directories[/FONT]
[FONT="] For Each directory In dirCollection[/FONT]
[FONT="] Debug.Print "---SubDirectory: " & directory & "---"[/FONT]
[FONT="] TraversePath path & directory & ""[/FONT]
[FONT="] Next directory[/FONT]
[FONT="] End Sub[/FONT]
[FONT="] Sub Test12()[/FONT]
[FONT="] TraversePath12 "C:\Users\VBA_Testing_Folder"[/FONT]
[FONT="] End Sub[/FONT]