Send Email from Specific File Path and Reference Text in Cell

skierpro720

New Member
Joined
Jul 20, 2018
Messages
2
I am trying to send files to email addresses in cell A1 of a file within a folder path. Cell A1 of every file contains an email address. I am struggling to call the text in that cell in the ".To" section of myu script. In addition, the second "With" statement ends up attaching files 1 and 2, rather that file 2 exclusively. I would also like to disable the Outlook security warning I receive about another program trying to access Outlook. I would appreciate any help!


Sub Send_File_Outlook()


Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim SubjectLine As String

SubjectLine = "Subject A"

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Application.DisplayAlerts = False

strbody = "Line 1" & vbNewLine & vbNewLine & _
"Line 2"



On Error Resume Next
With OutMail
.To = Range("A1").("\\Filepath\FileName_1.xlsx")
.CC = ""
.BCC = ""
.Subject = SubjectLine
.Body = strbody
.Attachments.Add ("\\Filepath\FileName_1.xlsx")
.Display
.Send
End With
With OutMail
.To = Range("A1").("\\Filepath\FileName_2.xlsx")
.CC = ""
.BCC = ""
.Subject = SubjectLine
.Body = strbody
.Attachments.Add ("\\Filepath\FileName_2.xlsx")
.Display
.Send
End With
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I've sent email multiple times using Excel VBA, but I've never tried to create multiple emails in one pass. Perhaps there's a way to do this with a loop of some kind?

If you have an email address in A1, why do you need the "
.("\\Filepath\FileName_1.xlsx")"? I would think Range("A1").Value would do the trick.

And I think you have to reset your OutMail object to nothing before you redefine it in the second instance. That way there's no remnant of the attachment. So, between "End With" and "With", try adding "
Set OutMail = Nothing"
<strike></strike>

Something like this?

Code:
[LEFT][COLOR=#333333][FONT=Verdana][B][I][U][SUB][SUP]<strike>
</strike>[/SUP][/SUB][/U][/I][/B]Sub Send_File_Outlook()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String
    Dim SubjectLine As String

    SubjectLine = "Subject A"
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Application.DisplayAlerts = False
    strbody = "Line 1" & vbNewLine & vbNewLine & "Line 2"

    On Error Resume Next

    With OutMail
        .To = Range("A1").Value
        .CC = ""
        .BCC = ""
        .Subject = SubjectLine
        .Body = strbody
        .Attachments.Add ("\\Filepath\FileName_1.xlsx")
        .Display
        .Send
    End With
    
    Set OutMail = Nothing
    Set OutMail = OutApp.CreateItem(0)
    
    With OutMail
        .To = Range("A1").Value
        .CC = ""
        .BCC = ""
        .Subject = SubjectLine
        .Body = strbody
        .Attachments.Add ("\\Filepath\FileName_2.xlsx")
        .Display
        .Send
    End With
    
End Sub
[/FONT][/COLOR][/LEFT]

HTH
atroxell
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top