【发布时间】:2017-06-14 10:01:41
【问题描述】:
我正在尝试从活动电子表格中的单元格中绘制可编辑的输入。我设法在 Outlook 中生成了一封电子邮件并附加了多个文件。
问题在于,不是在每个单元格中循环使用数据(和不同的目录输入),从而附加“x”个单独的文件,而是附加相同的文件“x”次。
Option Explicit
Sub TESTCreateEmail()
'define variables for outlook
Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
'define variables for attachements to email
Dim wb As Workbook
Dim ws As Worksheet
Dim LoopAttach As Integer
Dim x As Integer
Dim y As Integer
x = Range("E9", Range("E100").End(xlUp)).Count
'set parameters for outlook
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
'set parameters for attachements to email
Set wb = ThisWorkbook
Set ws = wb.Application.ActiveSheet
'create email
With olMail
.To = ws.Range("H9")
.CC = ws.Range("I9")
olMail.SentOnBehalfOfName = "email@email.com"
olMail.Recipients.ResolveAll
.BodyFormat = olFormatHTML
olMail.HTMLBody = "<html><p><font face=""Calibri""><font size=3>Dear Sir/ Madam,</p>"
'loop to attach multiple files
For LoopAttach = 1 To x
.Attachments.Add ws.Range("E9").Offset(x - 1, 0) _
& ws.Range("F9").Offset(x - 1, 0)
Next LoopAttach
.Display
End With
End Sub
【问题讨论】:
标签: excel vba for-loop outlook email-attachments