将Outlook中的“收件人”字段指向单元格

我正试图在Excel VBA上创build一个macros,它将创build一个电子邮件并从Excel单元格K6填充To字段。

当这个代码运行时,我得到错误消息Run-time error'5': Invalid procedure call or argument

 Dim OutApp As Object Dim MItem As Object Dim cell As Range Dim rng As Range Dim Subj As String Dim EmailAddr As String Dim myRecipient As Object Dim myRecipients As Object Dim Recipient As String Dim Msg As String Dim ws1 As Worksheet Dim DateNow As Date Set ws1 = Sheets("Email") 'Create Outlook object Set rng = ws1.Range("B6:F26").SpecialCells(xlCellTypeVisible) With Application .EnableEvents = False .ScreenUpdating = False End With Set OutApp = CreateObject("Outlook.Application") Set MItem = OutApp.CreateItem(0) Set myRecipients = MItem.Recipients myRecipients = ws1.Cells.Range("K6") If Not myRecipients.ResolveAll Then For Each myRecipient In myRecipients If Not myRecipient.Resolved Then MsgBox myRecipient.Name End If Next End If DateNow = Format(Now, "dd/MM/yyyy") DateNow2 = Format(Now, "h:mm") Msg = "This report was generated on " & DateNow & " at " & DateNow2 & "." With MItem .CC = EmailAddr2 .Subject = Subj .HTMLBody = RangetoHTML(rng) & Msg .Display End With On Error GoTo 0 With Application .EnableEvents = True .ScreenUpdating = False End With Set MItem = Nothing Set OutApp = Nothing End Sub 

如果我使用Set myRecipients = ws1.Cells.Range("K6") ,则得到错误消息Run-time error '438': Object doesn't support this property or method

如果我将myRecipients As String设置myRecipients As String ,则说明Object required

我有很多问题来理解在Excel VBA中绑定Outlook的时间,而且我已经阅读了很多东西,但是还没有find很多可以用更加教导的方式来解释这个问题的源代码。

除此之外,我还试图在添加单元格的内容之后parsing(在Outlook上使用ctrl + K将电子邮件parsing为显示名称的效果)添加到“ To字段的电子邮件,但我可以没有做第一部分的工作就没有testing。

感谢您的关注,

编辑:布鲁斯·韦恩的build议后,我把它们作为Range ,但现在我得到一个不同的错误: Run-time error '-2147352567 (800200009)': Property is read-only.

Dim myRecipient As Range Dim myRecipients As Range

在代码中间:

 Set OutApp = CreateObject("Outlook.Application") Set MItem = OutApp.CreateItem(0) Set myRecipients = ws1.Cells.Range("K6") Set MItem.Recipients = myRecipients 

德米特里的build议之后:

 Set OutApp = CreateObject("Outlook.Application") Set MItem = OutApp.CreateItem(0) Set myRecipients = ws1.Cells.Range("K6") Set myRecipient = MItem.Recipients.Add(myRecipients) If Not myRecipients.ResolveAll Then For Each myRecipient In myRecipients If Not myRecipient.Resolved Then MsgBox myRecipient.Name End If Next End If 

但是我得到错误信息: Run-time error '438': Object doesn't support this property or method标记在If Not myRecipients.ResolveAll Then 。 如果我删除所有的If部分,代码运行良好。 但是,我能够parsingTo / CC字段中的姓名和电子邮件对我来说非常重要。

收件人属性的确是只读的。 您需要为每个收件人调用MailItem.Recipients.Add或将To / CC / BCC属性设置为“;” 分隔的名字或地址列表。

更新:

 Set OutApp = CreateObject("Outlook.Application") Set MItem = OutApp.CreateItem(0) Set recipName = ws1.Cells.Range("K6").Value Set myRecipient = MItem.Recipients.Add(recipName) If Not myRecipient.Resolve Then MsgBox myRecipient.Name End If 

我认为这是因为你将myRecipientsmyRecipient设置为Object,但是想要将它设置为Rangetypes。 尝试:

 Dim myRecipients as Range, myRecipient as Range Dim objMyRecipients as Object, objMyRecipient as Object 'create a variable that holds the object 

然后,当你需要使用它们作为一个对象时,你有一个单独的variables来做到这一点。