将Outlook中的“收件人”字段引用到单元格

时间:2015-11-17 18:30:50

标签: excel vba excel-vba

我正在尝试在Excel VBA上创建一个用于创建电子邮件的宏,并将填充Excel单元格To中的K6字段。

当此代码运行时,我收到错误消息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,则会显示Object required

我在解决Excel VBA中的后期绑定Outlook方面遇到了很多问题,我已经阅读了很多内容,但是没有找到很多可以用更多教学方式解释这个问题的资源。

除此之外,我还试图在添加单元格的内容后,解决(在Outlook上使用ctrl + K解析电子邮件到显示名称的效果)添加到{{1的电子邮件}}但是我不能在不使第一部分工作的情况下测试它。

感谢您的关注,

编辑:在Bruce Wayne的建议之后,我将它们设为To,但现在我收到了另一个错误: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

但是我收到了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部分,代码运行正常。但是对我来说非常重要的是我能够在To / CC字段中解析名称和电子邮件。

2 个答案:

答案 0 :(得分:2)

收件人属性确实是只读的。您需要为每个收件人调用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

答案 1 :(得分:0)

我认为这是因为您将myRecipientsmyRecipient设置为对象,但希望将其设置为基本上为Range的内容类型。尝试:

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

然后,当您需要将它们用作对象时,您有一个单独的变量来执行此操作。