2015-10-07 24 views
0

我有一長串成本報告要發送給不同的收件人。將特定文件附加到相應的收件人

我想我可以有一個用的Excel文件的地址和相應的位置即 A1 [email protected] A2 0001 B1 [email protected] B1技術

然後使用VBA循環每行(1)並搜索相應的(A2)命名文件的文件夾並將其附加到郵件輸出到單元格(A1)。

+0

你是在正確的軌道上。 A欄有電子郵件。 B列有文件位置。循環訪問列A以獲取電子郵件和列B附加。這個鏈接是一個很好的開始。 http://stackoverflow.com/questions/17883088/excel-vba-sending-mail-using-outlook-send-method-fails。 (不知道爲什麼用戶對'.Send'有問題,但它總是適用於我。)因此,在工作簿中設置一個循環,並應用列A中的「To」和列B中的「Attachment」,並填充其餘部分如所須。 –

回答

1

我假設你在第一行有標題。 未經測試。

Sub AntMan() 

Dim OutLookApp As Object 
Dim OutLookMailItem As Object 
Dim lastRow As Long 
Dim MailDest As String 
Dim subj As String 

lastRow = ThisWorkbook.WorkSheets("Sheet6").Cells(Rows.Count, "A").End(xlUp).Row 'change worksheet 

For i = 2 To lastRow 

    Set OutLookApp = CreateObject("Outlook.application") 
    Set OutLookMailItem = OutLookApp.CreateItem(0) 
    Set Attach = OutLookMailItem.Attachments 

    With OutLookMailItem 
     .To = Cells(i, 1).Value 
     .SUBJECT = "Put your subject here" 
     .Body = "Put your body here" 
     Attach.Add "C:\your\file\path\here\" & Cells(i, 2).Value & ".xlsx" 
     .Display 'for debugging 
     .Send 
    End With 

Next 

End Sub 
+0

你沒有前景?其他電子郵件客戶端可以像「Windows Live 2012」或「Thunderbird」一樣使用。 – HarveyFrench

+0

@HarveyFrench OP沒有指定。 – findwindow

+0

謝謝像魅力一樣工作。我正在使用Outlook。 – AntMan

相關問題