vba 自动生成电子邮件无法解析多个收件人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13019651/
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 18:10:24  来源:igfitidea点击:

Automated email generation not resolving multiple recipients

vbaoutlookaccess-vbaoutlook-2007

提问by PowerUser

I have a VBA script which creates & saves Draft emails. To add recipients, it pulls a string from a linked Excel table and adds that to the Recipients object.

我有一个创建和保存草稿电子邮件的 VBA 脚本。要添加收件人,它会从链接的 Excel 表中提取一个字符串并将其添加到收件人对象中。

For emails with single recipients, this works like a charm. All the user needs to do is open the draft, spend 5 seconds looking it over, and hit Send.

对于具有单个收件人的电子邮件,这就像一个魅力。用户所需要做的就是打开草稿,花 5 秒钟查看它,然后点击发送。

The problem occurs with several contacts at once (e.g. "[email protected]; [email protected]; [email protected]"). When the user hits Send, Outlook will pop up a Check Names dialogue with no suggestions. The user can get around this by clicking on the To field and entering a dummy semicolon to trigger the auto resolve. I'd like to avoid this since this process creates well over a hundred emails at a time which need to be individually reviewed.

多个联系人同时出现问题(例如“[email protected][email protected][email protected]”)。当用户点击发送时,Outlook 将弹出一个没有建议的检查名称对话框。用户可以通过单击“收件人”字段并输入虚拟分号来触发自动解析来解决此问题。我想避免这种情况,因为这个过程一次会创建一百多封需要单独的电子邮件。

Looking around on the net, I've found and tried Recipients.ResolveAll which returns false. I suspect the reason is that Outlook is trying to resolve the entire string of recipients at once and not individually. So my question is: how do I get Outlook to stop displaying this Check Names dialogue? Do I need to loop thru my email string and parse out the individual emails?

在网上环顾四周,我发现并尝试了返回 false 的 Recipients.ResolveAll。我怀疑原因是 Outlook 试图一次解析整个收件人字符串,而不是单独解析。所以我的问题是:如何让 Outlook 停止显示此“检查名称”对话框?我是否需要遍历我的电子邮件字符串并解析出各个电子邮件?

Sub CreateEmail(id as Integer)
    Dim OlApp As Outlook.Application
    Dim ObjMail As Outlook.MailItem
    Dim Recipients As Outlook.Recipients
    Dim CurrentRecipient As Outlook.Recipient

    Set OlApp = CreateObject("Outlook.Application")
    Set ObjMail = OlApp.CreateItem(olMailItem)
    Set Recipients = ObjMail.Recipients

    Dim StrEmailTo As String
    StrEmailTo = CurrentDb.OpenRecordset( _
        "Select [Emails] from LU_Contacts where id=" & id & ";").Fields(0)

    Set CurrentRecipient = Recipients.Add(StrConv(StrEmailTo, 3))
    CurrentRecipient.Type = olTo
    ...

    Objmail.Save

回答by SeanC

Recipients.Addtakes a single email address.

Recipients.Add需要一个电子邮件地址。

If you wish to have multiple recipients, call Recipients.Addfor each one.

如果您希望有多个收件人,请Recipients.Add为每个收件人致电。

If your string is returned in a ;delimited format, then something like:

如果您的字符串以;分隔格式返回,则类似于:

dim EmailList as variant
dim NumEmails as long
dim AddEmailLoop as long

EmailList=split(StrEmailTo,";")
NumEmails=UBound(EmailList)

For AddEmailLoop=0 to NumEmails
    Recipients.add(EmailList(AddEmailLoop))
next

should allow you to add the entire list

应该允许您添加整个列表