2017-06-15 12 views
0

私はこのトピックを研究し、素晴らしいコードを発見しました。私は300人の受信者への添付ファイルの電子メール配信のための範囲を設定するExcelファイルを作成しました。これはうまくいきます。しかし、私は同じ受信者に行く必要がある複数の添付ファイルがあります。列Aは、ファイル名が選択されたフィールドです。受信者1のpdfを取得します。受信者1の2番目のpdfファイルには列Bを使用できますか?MultiAttachment複数の受信者へのファイルの配布

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 

答えて

0

このようにそれを試してみてください。

とシート(「シート1」)でリストを作成します:「シート1」の各行によるマクロがループ

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) 

、列B とファイル名にE-mailアドレスがある場合列C:Zには、この情報を含むメールが作成されて送信されます。

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

関連する問題