Vba 参见';至';Outlook中的字段转换为单元格

Vba 参见';至';Outlook中的字段转换为单元格,vba,excel,Vba,Excel,我正在尝试在Excel VBA上创建一个宏,该宏将创建一封电子邮件,并将从Excel单元格K6填充to字段 当此代码运行时,我收到错误消息运行时错误'5':过程调用或参数无效 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

我正在尝试在Excel VBA上创建一个宏,该宏将创建一封电子邮件,并将从Excel单元格
K6
填充
to
字段

当此代码运行时,我收到错误消息
运行时错误'5':过程调用或参数无效

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”)
我会收到错误消息
运行时错误“438”:对象不支持此属性或方法

如果我将
myRecipients设置为String
,它会显示
所需对象

我在理解ExcelVBA中的后期绑定Outlook时遇到了很多问题,我读过很多东西,但还没有找到更多关于它的资料,可以用更具说教性的方式来解释这一点

除此之外,我还尝试在添加单元格内容后,解析(在Outlook上使用ctrl+K将电子邮件解析为显示名称的效果)添加到
to
字段的电子邮件,但如果不让第一部分正常工作,我无法测试它

谢谢大家的关注

编辑:根据Bruce Wayne的建议,我将它们设置为
范围
,但现在我得到了一个不同的错误:
运行时错误'-2147352567(800200009)':属性是只读的。

Dim myRecipient作为范围
将我的收件人设置为范围

在代码中间:

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

但我收到错误消息:
运行时错误“438”:对象不支持在
上标记的该属性或方法,如果不是myRecipients.ResolveAll,则为
。如果删除所有If部分,代码运行良好。但对我来说,能够解析“收件人/抄送”字段中的姓名和电子邮件是非常重要的。

我认为这是因为您将
myRecipients
myRecipient
设置为对象,但希望将其设置为本质上属于
范围的类型。尝试:

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

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

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

这是有道理的,但我做了一些更改,现在我收到了
运行时错误“-2147352567(800200009)”:属性是只读的。
我会将我所做的更改添加到原始帖子中以进一步澄清。如果不解决所有
部分,我只是在执行
时遇到了困难。它表示
myRecipients
不支持该属性或方法。但我认为我现在所做的没有多大意义:
如果不是myRecipients.ResolveAll,则为myRecipients中的每个myRecipient如果不是myRecipient.Resolved,则为MsgBox myRecipient.Name,而myRecipients是一个单元格。。。我在帖子中更新了它,以便您可以更清楚地看到它,因为上面代码中的myRecipients变量指向Excel中的一个范围,不是从MauleM.ReliffsDebug检索的Outlook。收件人对象的实例,但如何让它考虑在从Excel单元格检索信息并将其插入到字段中之后,它将测试收件人是否被解析。您需要修复代码中的错误。看到我更新的帖子了。很好!它工作得很好,但每次只发送一封电子邮件。如果我的K6单元格是
名称。surname@company.com
它将解决问题。如果是
名称。surname@company.com; 姓名2。surname2@company.com
它无法解决。我试着移动
但仍然没有结果。因为它是。收件人,我想它需要不止一个,对吗?