Link to home
Start Free TrialLog in
Avatar of Nirvana
NirvanaFlag for India

asked on

Send E-mail along with attachment based on Filename sktneer

Hi Neeraj sorry to bug you on a closed question. if i have multiple customers with same name and i add serial numbers as 1.jerry 2. jerry can you we add that. here is the final code that i have. thank you

Dim fso As Object
Dim folderPath As String
Dim Folder As Object
Dim File As Object
Dim fileName As String
Sub SendEmailWithAttachment()
Dim olApp As Object
Dim olEmail As Object
Dim rng As Range, cell As Range
Dim lr As Long

Application.ScreenUpdating = False

'Path of the folder containing pdf files
folderPath = Environ("UserProfile") & "\Desktop\infi\"

lr = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("A2:A" & lr)

Set fso = CreateObject("Scripting.FileSystemObject")
Set olApp = CreateObject("Outlook.Application")

'Check if the Folder with pdf file exists
If Not fso.FolderExists(folderPath) Then
   MsgBox "Folder " & folderPath & " doesn't exist.", vbCritical, "Folder Not Found!"
   Exit Sub
End If
For Each cell In rng
   fileName = pdfFileName(cell.Value)
   If fileName <> "" Then
      With olApp.createitem(0)
         .to = cell.Offset(0, 1).Value
         .cc = cell.Offset(0, 2).Value
         .Subject = cell.Offset(0, 3).Value
         .body = Range("F1").Value
         .attachments.Add folderPath & fileName
         .SentOnBehalfOfName = Range("G2").Value
         '.send
         .display
      End With
   End If
Next cell
Application.ScreenUpdating = True
Set olApp = Nothing
Set fso = Nothing
End Sub

Function pdfFileName(vFileName As String) As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set Folder = fso.getfolder(folderPath)
For Each File In Folder.Files
   If InStr(File.Name, vFileName) > 0 And fso.GetExtensionName(File.Name) = "pdf" Then
      pdfFileName = File.Name
      Exit For
   End If
Next File
End Function

Open in new window

SendEmailWithAttachment_v2withfrom1.xlsm
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

What will change in this scenario?
i.e. when you have multiple same names with different serial nos. in col. A, are the pdf files also named with serial nos. and the customer's name?

When you ran the code with the new setup what went wrong?
Avatar of Nirvana

ASKER

When I am having multiple invoices for the same customer it is only picking up one pdf document from the folder

what will change is in Col A i will have customer name however in the folder i will have customer name prefixing serial number

for example i will still have Jerry, Sue etc., in Col A but in file name i will have
1.Jerry_ invoice number,
2.Jerry_ invoice number

1.Sue_ invoice number
2.Sue__ invoice number

or if it can pick all the one starting with Jerry sue.. etc in Col A

now if i have five files starting with Jerry it is only picking one
That's correct. If you have five files starting with Jerry, it will pick the one file only.
But if you have customers like 1. Jerry, 2. Jerry etc and if the files are named like 1. Jerry_invoice#, 2. Jerry_incoice#, the code will pick the file 1. Jerry_invoice# in case of the customer 1. Jerry and pick the file 2. Jerry_invoice# in case of the customer 2. Jerry.

Did you test the code with the above explained setup?
Avatar of Nirvana

ASKER

I did still no luck. so you are saying as below right. it is still picking only one. Can you help me to pick all?
1.Jerry_invoice number
1.Jerry_invoice number
Upload the workbook with the modified customer names in col. A and let me know the name of the files based on the one customer you want the code to pick.
Avatar of Nirvana

ASKER

Here is the attached file with customer name in Col A. you can take any customer
SendEmailWithAttachment_v2withfrom1.xlsm
You said col. A may contain multiple same names with serial nos., in your file this is not the case.

In your file based on the customer ClickTale, what would be the name possible pdf files you want to attach?
Do you want to attach all of them with a single customer in this case ClickTale?
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nirvana

ASKER

thanks again brilliant as usual. how can i learn VB to develop like these  myself?
Avatar of Nirvana

ASKER

hey hi.. sorry to bug you so much never thought this would require so many changes. is there a way that i can i have a new email for each invoice rather than all invoices (pdfs) in one mail.

really sorry and thanks again
Try the code in the attached and see if this is what you are trying to achieve.
SendEmailWithAttachment_v3.xlsm
Avatar of Nirvana

ASKER

can't thank you enough sir. works perfect
You're welcome Uday!