Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I have two type of files to be attached in same male, let's say one is A and second is B but A & B never in the same quantity but mail should be prepared according to A

What I have tried:

VB
Sub SendEmailObligation()
	Application.DisplayAlerts = False
	Application.ScreenUpdating = False
	
	Workbooks("Auto Mail Schedule.xls").Activate
	Sheets("Client mail").Select
	
	For i = 2 To Range("a1").SpecialCells(xlCellTypeLastCell).Row
		
		AttachFileName1 = Cells(i, 5).Value & Cells(i, 6).Value
		AttachFileName2 = Cells(i, 5).Value & Cells(i, 7).Value
		
		If Dir(AttachFileName1) <> "" Then 'And Dir(AttachFileName2) <> "" Then
			
			ESubject = Cells(i, 3).Value & " Exchange Obligation Report for Delivery Date " & Format$(Date + 1, "MMMM dd, yyyy") & "."
			SendTo = Cells(i, 1).Value
			CCTo = Cells(i, 2).Value
			
			Ebody = "Dear Sir/Ma'am," & Chr(10) & Chr(10) & _
			"Kindly find the attached exchange result for the day." & Chr(10) & Chr(10) & _
			Chr(10) & Chr(10) & "The obligation reports are also attached herewith." _
			& Chr(10) & Chr(10) & "Thanks & Regards," _
			& Chr(10) & Chr(10) & "GMRETL Control Room" _
			& Chr(10) & "New Shakti Bhawan,Opposite ATS complex" _
			& Chr(10) & "IGIA New Delhi-110037 " _
			& Chr(10) & "Phone: Off: 011-49883320,Fax: Off: 011-49882255/49883331 " _
			
			Set App = CreateObject("Outlook.Application")
			Set Itm = App.CreateItem(0)
			With Itm
				.Subject = ESubject
				.To = SendTo
				.CC = CCTo
				.Body = Ebody
				.Attachments.Add (AttachFileName1)
				.Attachments.Add (AttachFileName2) ' Must be complete path
				.Save
'.Send
			End With
			Set App = Nothing
			Set Itm = Nothing
		End If
	Next
	MsgBox ("All Clients mails Ready to be Sent!!! ")
End Sub
Posted
Updated 28-Oct-17 23:43pm
v2
Comments
phil.o 29-Oct-17 5:45am    
"A & B never in the same quantity but mail should be prepared according to A"
This is confusing :) Could you clarify this statement, please? Do you mean that there is always an A, but only a B on some occasions?
A_Griffin 29-Oct-17 6:07am    
WHat's the problem? Only add a second attachment if it exists.
BTW, instead of Chr(10) & Chr(10) you could use vbCrLf
Even better, use a StringBuilder with .AppendLine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900