I am running a macro for sending mails to multiple recipients via Outlook with one or more attachments through vba excel. I am not well versed in macros and hence took some inputs from various sources and came upon the below final code.
However I have mentioned max. limit of 3 file attachments which is constant for all recipients but have to disable by commenting whenever I have to attach only 1 or 2 files accordingly like e.g in the below code I have disabled the 2nd and 3rd attachment columns for attaching 1 file across. Is there any way where the macro would automatically take the inputs according to the values entered and left blank e.g If one recipient has 1 attachment and the next recipient has 2 or 3 attachments
Sub SendMail() Dim objOutlook As Object Dim objMail As Object Dim ws As Worksheet Set objOutlook = CreateObject("Outlook.Application") Set ws = ActiveSheet For Each cell In ws.Range("A2:A1000") Set objMail = objOutlook.CreateItem(0) With objMail .To = cell.Value .Cc = cell.Offset(0, 1).Value .Bcc = cell.Offset(0, 2).Value .Subject = cell.Offset(0, 3).Value .Body = cell.Offset(0, 4).Value .Attachments.Add cell.Offset(0, 5).Value '.Attachments.Add cell.Offset(0, 6).Value '.Attachments.Add cell.Offset(0, 7).Value .Send End With Set objMail = Nothing Next cell Set ws = Nothing Set objOutlook = Nothing End Sub