【发布时间】:2020-06-24 04:02:52
【问题描述】:
我有一个 Excel 表,范围 A1:B7 中有以下值
+----------------+--------------------+
| Recipient Type | Recipient Addresss |
+----------------+--------------------+
| To | a@xyz.com |
| To | b@xyz.com |
| CC | c@xyz.com |
| CC | d@xyz.com |
| BCC | e@xyz.com |
| BCC | f@xyz.com |
+----------------+--------------------+
然后我制作了以下 VBA 宏以将这些添加为 Outlook 中的电子邮件收件人
Option Explicit
Sub Add_Recipients_Data_and_Type()
Dim olApp As Outlook.Application
Set olApp = GetObject(, "Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.Display
Dim rn As Range
Dim cl As Range
Dim i As Long
i = 1
Set rn = Range("A1").CurrentRegion.Columns(1).Range(Cells(1, 1), Cells(Range("A1").CurrentRegion.Rows.Count, 1))
For Each cl In rn
Select Case cl.Value
Case "To"
olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olTo
Case "CC"
olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olCC
Case "BCC"
olMail.Recipients.Add(cl.Offset(0, 1).Value).Type = olBCC
End Select
i = i + 1
Next cl
End Sub
问题是.. 最后一个收件人,即 f@xyz.com 总是被添加到收件人字段而不是密件抄送字段。 但是,如果我在表中创建 一个虚拟的空白最后一行条目,如下所示 Recipient Type = BCC 和 Recipient Address = " "(一个空格),然后代码工作并按原计划在 To、CC 和 BCC 字段中添加两个收件人
可能是什么原因?
【问题讨论】:
-
在第一步中,您可以在
For Each cl In rn设置断点并在下面添加Debug.Print cl.Value。现在执行代码,当它到达断点时,使用 F8 单步执行并检查值。您应该在即时窗口中看到实际值。