Having trouble debugging the code below.
I'm trying to automate a macro to send multiple attachments based on a named range.
Sub Test()
Dim objol As New Outlook.Application, objMail As MailItem
Dim MyArr As Variant, i As Long
Set objol = New Outlook.Application
Set objMail = objol.CreateItem(olMailItem)
With objMail
MyArr = Sheets("Sheet1").Range("A2:A9").Value
.To = ("[email protected]")
.Subject = "Test"
.Body = ""
.NoAging = True
For i = LBound(MyArr) To UBound(MyArr)
If Dir(MyArr(i, 1), vbNormal) <> "" Then .Attachments.Add MyArr(i, 1)
Next i
.Display
End With
End Sub
In the example I'm testing, I just have two inputs in the range ("Sheet2" and "Sheet3" in cells A2 & A3 respectively). It seems like the code acts up at i=3 where the row is blank. But I need that to be okay. As the column it's referring to is set (A2:A9), the user puts in names of worksheets they want to email found in the workbook. Sometimes the user can input 2 names, or 3 names - any amount up to A9. I just need the code to End the looping if there's a blank in the range, and send the attachments already defined in the range.
As of now, it keeps giving me an type mismatch error? (Type mismatch happens at If Dir(MyArr(i, 1), vbNormal) <> "" Then .Attachments.Add MyArr(i, 1)
Edit - could also be an issue due to Dir - the values in the range are the sheet names, so Sheet1, Sheet2
A2:A9are literally only sheet namesDirwill not work, neither will.Attachments.Add, as they both expect a valid file path name. 2) To exit out of the loop when a cell is blank you can writeIf myArr(i,1) = vbNullStringfor example. There other ways to do it, including just loop through the range itself. Data set is so small it makes no real difference in this case. - Scott HoltzmanAttachments.Addtake in Sheet names? Could something like this work:Set rRange = Sheets("Sheet1").Range("A2:A9") For Each rcell In rRange If rcell.Value <> "" Then Sheets(rcell.Value).Copy End If Debug.Print rcell.Value- S31