vba Recipients.Add 生成运行时错误“287”:应用程序定义或对象定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1265270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:41:07  来源:igfitidea点击:

Recipients.Add generates Runtime error '287': Application-defined or object-defined error

vbaemailoutlook

提问by Steve

I am testing how to send an e-mail. I have copied the code below from the help files:

我正在测试如何发送电子邮件。我已经从帮助文件中复制了以下代码:

Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties'

    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
    'Create e-mail item'
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
        .Subject = "Test Message"
        .Body = "Body Text"
        .Recipients.Add "[email protected]"
        .Recipients.ResolveAll
        .Display
    End With
End Sub

I receive a Runtime error '287' message with the .Recipients.Add line highlighted. What am I doing wrong?

我收到一条运行时错误“287”消息,其中突出显示了 .Recipients.Add 行。我究竟做错了什么?

回答by Treb

Edit:
As the OP states in his comment to my original answer, changing his code to

编辑:
正如 OP 在他对我的原始答案的评论中所说的那样,将他的代码更改为

.Recipients.To = "[email protected]" 

solved his problem. I leave my original answer below, because someone may learn from the mistake I made, pointed out by divo;-)

解决了他的问题。我在下面留下我的原始答案,因为有人可能会从我犯的错误中吸取教训,由div指出;-)



Original answer (careful, this is wrong!):

原始答案(小心,这是错误的!):

Try enclosing the parameters passed to the Addmethod with parentheses:
.Recipients.Add ("[email protected]")

尝试将传递给Add方法的参数用括号括起来:
.Recipients.Add ("[email protected]")

回答by Tommy O'Dell

Try this:

尝试这个:

   toString = "[email protected];[email protected];[email protected]"

    With OutMail
        .To = toString
        .Subject = "Hello Friends"
        .Body = "Here is the email body"
        .Send
    End With

This of course works with multiple recipients. For a single recipient, just do this:

这当然适用于多个收件人。对于单个收件人,只需执行以下操作:

toString = "[email protected]"

And don't forget the .Sendto actually make your email send.

并且不要忘记.Send实际发送您的电子邮件。