我正在使用 VBA 代碼從 Excel 作業表發送多封電子郵件,如下例所示:
示例 Excel 作業表

到目前為止,我能夠使用代碼為 C 列中的每個電子郵件地址生成一個單獨的電子郵件,并附加一個檔案。我的代碼如下 - 我希望添加允許我執行以下操作的代碼:
- 將 D 列中的日期拉入電子郵件正文。我已經在我想要日期的電子郵件正文中輸入了代碼,但不知道如何逐行獲取每封電子郵件的日期。
- 如果 E 列中有鏈接,請在電子郵件中附加一個附加檔案。這將是第二個附件,除了代碼中已有的附件。
Excel 作業表中的每一行都應使用 C 列中的電子郵件地址、D 列中的日期和 E 列中的附件生成單獨的電子郵件。
Sub CreateEmails()
Dim sourceWorksheet As Worksheet
Set sourceWorksheet = Worksheets("Sheet1")
Dim lastRow As Long
With sourceWorksheet
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
End With
Dim OutlookApp As Object
Set OutlookApp = CreateObject("Outlook.Application")
Dim rowIndex As Long
For rowIndex = 2 To lastRow 'start at the second row
Dim MItem As Object
Set MItem = OutlookApp.CreateItem(0)
With MItem
.To = sourceWorksheet.Cells(rowIndex, "C").Value
'Will pull in all email address in Row C as separate emails
.Subject = "Subject Here"
.Attachments.Add “first attachment link goes here"
.htmlBody = "<p><font face = ""Calibri(Body)"" font size=""3"" color=""black"">Good afternoon, </p>" & _
"<p><font face = ""Calibri(Body)"" font size=""3"" color=""black""><strong>Please review the attached and return by" & DueDate & ". </strong> </p>"& .htmlBody
.display
End With
Next rowIndex
End Sub
uj5u.com熱心網友回復:
假設其余代碼可以作業,只需將 with-block 更新為:
With MItem
.To = sourceWorksheet.Cells(RowIndex, 3).Value
.Subject = "Subject Here"
If Cells(RowIndex, 5) <> "" Then .Attachments.Add Cells(RowIndex, 5).Value
If Cells(RowIndex, 6) <> "" Then .Attachments.Add Cells(RowIndex, 6).Value
.HTMLBody = "<font face = ""Calibri(Body)"" font size=""3"" color=""black""><p>Good afternoon, </p>" & _
"<p><strong>Please review the attached and return by " & Cells(RowIndex, 4) & ". </strong> </p></font>" & .HTMLBody
.Display
End With
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496248.html
下一篇:如何洗掉文本的段落?
