2017-06-15 12 views
0

我研究了這個主題,發現了很好的代碼 - 但並不完全符合我的需求。我已經創建了一個Excel文件來爲一個附件的電子郵件分發範圍設置爲3百個收件人 - 這很好。但我有多個附件需要去同一個收件人。列A是選擇文件名稱的字段 - 它爲接收方1選擇pdf。對於接收方1是否可以使用列B作爲第二個pdf文件,以及如何將其循環?MultiAttachment varing文件分發給多個收件人

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

嘗試這種方式。

請在表(「工作表Sheet1」)的列表:

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) 

通過「工作表Sheet1」每一行和宏將循環,如果在B列 和文件名的E-mail地址(C)列:它將創建一封包含此信息的郵件併發送。

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

相關問題