2017-06-15 4 views
0

J'ai étudié ce sujet et trouvé du code génial - mais pas tout à fait ce dont j'ai besoin. J'ai créé un fichier Excel pour configurer une plage pour la distribution d'une pièce jointe à trois cents destinataires - ce qui fonctionne bien. Mais j'ai plusieurs pièces jointes qui doivent aller au même destinataire. Colonne A est le champ où le nom de fichier est choisi - qui prend un pdf pour le destinataire 1. Est-il possible d'utiliser la colonne B pour le deuxième fichier pdf pour le destinataire 1 et comment puis-je boucler cela?MultiAttachment Distribution de fichiers de varistance à plusieurs destinataires

https://i.stack.imgur.com/huVRy.png

Sub Mail_Report() 
    Dim OutApp As Object 
    Dim OutMail As Object 

'Use presence of a Path to determine if a mail is sent. 
    Set Rng = Range(Range("J2"), Range("J" & Rows.Count).End(xlUp)) 
    For Each cell In Rng 
    Rw = cell.Row 

    Path = cell.Value 
    If Path <> "" Then 
    'Get Date info from Path 
     'Dte = Right(Path, Len(Path) - InStrRev(Path, "\")) 

    'Get Territory to check for filename (Column A) 
     FilNmeStr = cell.Offset(0, -9).Value 
    'Email Address 
     ToName = cell.Offset(0, -5).Value 
    'Subject Line 
     SL = Cells(1, "K") 

    'Create Recipient List 
     For x = 1 To 4 
     Recp = cell.Offset(0, -x).Value 
     If Recp <> "" Then 
      Recp = cell.Offset(0, -x).Value 
     End If 
     RecpList = RecpList & ";" & Recp 
     Next 

     ccTo = RecpList 

    'Get Name 
     FirstName = cell.Offset(0, -7).Value 
     LastName = cell.Offset(0, -6).Value 

    'Loop through files in Path to see if 
     ClientFile = Dir(Path & "\*.*") 

     Do While ClientFile <> "" 
     If InStr(ClientFile, FilNmeStr) > 0 Then 
      AttachFile = Path & "\" & ClientFile  
      MailBody = "Hi " & FirstName & "," & vbNewLine & vbNewLine _ 
     End If 
     ClientFile = Dir 
     Loop 

     Set OutApp = CreateObject("Outlook.Application") 
     Set OutMail = OutApp.CreateItem(o) 
     With OutMail 
     .SentOnBehalfOfName = """TechSupport"" <[email protected]>" 
     .To = ToName 
     .cc = ccTo 
     .Subject = SL & " - " & cell.Offset(0, -9).Value 
     .Body = MailBody 
     .Attachments.Add (AttachFile) 
     .Display 
     '.Send 
     End With 
     Set OutMail = Nothing 
     Set OutApp = Nothing 
     RecpList = ""    
    End If 
    Next 
End Sub 

Répondre

0

Essayez cette façon.

Faire une liste Sheets (« Sheet1 ») avec:

In column A : Names of the people 
In column B : E-mail addresses 
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files) 

La boucle de volonté macro par chaque ligne « Sheet1 » et s'il y a une adresse e-mail dans la colonne B et le nom du fichier (s) dans la colonne C: Z, il créera un mail avec cette information et l'enverra.

Sub Send_Files() 
'Working in Excel 2000-2016 
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm 
    Dim OutApp As Object 
    Dim OutMail As Object 
    Dim sh As Worksheet 
    Dim cell As Range 
    Dim FileCell As Range 
    Dim rng As Range 

    With Application 
     .EnableEvents = False 
     .ScreenUpdating = False 
    End With 

    Set sh = Sheets("Sheet1") 

    Set OutApp = CreateObject("Outlook.Application") 

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants) 

     'Enter the path/file names in the C:Z column in each row 
     Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1") 

     If cell.Value Like "?*@?*.?*" And _ 
      Application.WorksheetFunction.CountA(rng) > 0 Then 
      Set OutMail = OutApp.CreateItem(0) 

      With OutMail 
       .to = cell.Value 
       .Subject = "Testfile" 
       .Body = "Hi " & cell.Offset(0, -1).Value 

       For Each FileCell In rng.SpecialCells(xlCellTypeConstants) 
        If Trim(FileCell) <> "" Then 
         If Dir(FileCell.Value) <> "" Then 
          .Attachments.Add FileCell.Value 
         End If 
        End If 
       Next FileCell 

       .Send 'Or use .Display 
      End With 

      Set OutMail = Nothing 
     End If 
    Next cell 

    Set OutApp = Nothing 
    With Application 
     .EnableEvents = True 
     .ScreenUpdating = True 
    End With 
End Sub 

https://www.rondebruin.nl/win/s1/outlook/amail6.htm