Add multiple attachments when the number of attachments changes

I send emails to about 150 individuals, each email can have from 1 to 3 attachments.

I can send emails just fine with one attachment ... getting multiple attachments is difficult.

Assume that the attachment file path is between A1 and C1.

How can i accomplish.

If A1 is empty, go to "Send", if not, add a file If B1 is empty, go to "Send", if not, add a file If C1 is empty, go to "Send", if not, attach a file

Submit:

This is the code I have now: I understand that my ranges are different from what I posted above. The following script works ... for only one application.

Set rngEntries = ActiveSheet.Range("b5:b172")

For Each rngEntry In rngEntries
    Set objMail = objOutlook.CreateItem(0)
    With objMail
        .To = rngEntry.Offset(0, 11).Value
        .Subject = rngEntry.Offset(0, 8).Value
        .Body = rngEntry.Offset(0, 10).Value
        .Attachments.Add rngEntry.Offset(0, 9).Value
        .send
    End With
Next rngEntry

What I want will look something like this.

Set rngEntries = ActiveSheet.Range("b5:b172")

For Each rngEntry In rngEntries
    Set objMail = objOutlook.CreateItem(0)
    With objMail
        .To = rngEntry.Offset(0, 11).Value
        .Subject = rngEntry.Offset(0, 8).Value
        .Body = rngEntry.Offset(0, 10).Value

If rngEntry.Offset(0, 1) is empty, goto Send

        .Attachments.Add rngEntry.Offset(0, 1).Value

If rngEntry.Offset(0, 2) is empty, goto Send

        .Attachments.Add rngEntry.Offset(0, 2).Value

If rngEntry.Offset(0, 3) is empty, goto Send

        .Attachments.Add rngEntry.Offset(0, 3).Value

Send: 
        .send
    End With

Next rngEntry
+4
1

GoTo VBA , . :

If Not IsEmpty(rngEntry.Offset(0, 1)) Then .Attachments.Add rngEntry.Offset(0, 1).Value

If Not IsEmpty(rngEntry.Offset(0, 2)) Then .Attachments.Add rngEntry.Offset(0, 2).Value

If Not ISEmpty(rngEntry.Offset(0, 3)) then .Attachments.Add rngEntry.Offset(0, 3).Value

, , | , , . , , .

: Outlook , , , . Early Binding, MS Office.

Option Explicit

Sub SendMail(strTo As String, strSubject As String, strBody As String, strAttachments As String, Optional strCC As String, Optional strFolder As String, Optional blSend As Boolean)
'requires declaration of Outlook Application outside of sub-routine
'passes file name and folder separately
'strAttachments is a "|" separate listed of attachment paths

Dim olNs As Outlook.Namespace
Dim oMail As Outlook.MailItem

'login to outlook
Set olNs = oApp.GetNamespace("MAPI")
olNs.Logon

'create mail item
Set oMail = oApp.CreateItem(olMailItem)

'display mail to get signature
With oMail
    .Display
End With

Dim strSig As String
strSig = oMail.HTMLBody

'build mail and send
With oMail

    .To = strTo
    .CC = strCC
    .Subject = strSubject
    .HTMLBody = strBody & strSig

    Dim strAttach() As String, x As Integer
    strAttach() = Split(strAttachments, "|")

    For x = LBound(strAttach()) To UBound(strAttach())
        If FileExists(strFolder & strAttach(x)) Then .Attachments.Add strFolder & strAttach(x)
    Next

    .Display
    If blSend Then .Send

End With

Set olNs = Nothing
Set oMail = Nothing

End Sub

FileExists, , , :

Function FileExists(sFile As String) As Boolean
'requires reference to Microsoft Scripting RunTime

Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(sFile) Then
    FileExists = True
Else
    FileExists = False
End If

Set fso = Nothing

End Function
+4

Source: https://habr.com/ru/post/1619082/


All Articles