VBA 使用 OFT 模板从 Excel 发送电子邮件 - 禁止自动签名

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

VBA send email from Excel using OFT template - Suppress automatic signatures

excelvbaoutlook-vba

提问by CactusCake

I've created a macro that opens an Outlook email template ready for the user to send (with some additional info pulled from the spreadsheet). All is going well except that Outlook is automatically appending a signature based on user settings. While I can turn off my own signature pretty easily, I'm not the only person that may be running this macro.

我创建了一个宏,可以打开一个 Outlook 电子邮件模板,供用户发送(从电子表格中提取一些附加信息)。一切都很顺利,只是 Outlook 会根据用户设置自动附加签名。虽然我可以很容易地关闭我自己的签名,但我并不是唯一可能运行这个宏的人。

The email template already has a company signature in it, so adding a personal signature as well is not wanted.

电子邮件模板中已经有公司签名,因此不需要添加个人签名。

Google offers plenty of suggestions for getting signatures ON to VBA generated emails, but I couldn't find anything to get them OFF. Any ideas?

谷歌提供了很多关于让 VBA 生成的电子邮件签名的建议,但我找不到任何东西来关闭它们。有任何想法吗?

Existing code:

现有代码:

... snip ...

Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItemFromTemplate("\Path\To\MyFile.oft")

With otlNewMail
.Bcc = vEmailsFromSpreadsheet '(set earlier on)
.Display
End With

'otlApp.Quit
Set otlNewMail = Nothing
Set otlApp = Nothing
Set otlAttach = Nothing
Set otlMess = Nothing
Set otlNSpace = Nothing

采纳答案by CactusCake

OK, so after much searching I figured out a way to achieve what I'm trying to do by combining a bunch of similar solutions to different problems.

好的,经过大量搜索,我找到了一种方法,通过将一堆类似的解决方案组合到不同的问题来实现我想要做的事情。

I learned that as long as you do not .Displaythe email then automatic user signatures do not get added. Therefore you can open (without displaying) the email template to copy the body, subject, and other email parameters you want from the template email into VB variables. Those variable values can then be used to overwrite parts of the email that you DO want to .Displayto the user.

我了解到,只要您不.Display发送电子邮件,就不会添加自动用户签名。因此,您可以打开(不显示)电子邮件模板以将模板电子邮件中的正文、主题和其他电子邮件参数复制到 VB 变量中。然后,这些变量值可用于覆盖您希望发送.Display给用户的电子邮件部分。

Here my final code:

这是我的最终代码:

Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItemFromTemplate("\Path\to\myfile.oft")
With otlNewMail
vTemplateBody = otlNewMail.HTMLBody
vTemplateSubject = otlNewMail.Subject
.Close 1
End With

Set otlApp = CreateObject("Outlook.Application")
Set otlNewMail = otlApp.CreateItem(0)
With otlNewMail
.Display
.SentOnBehalfOfName = vFrom
.Bcc = vToList
.Subject = vTemplateSubject
.HTMLBody = vTemplateBody
End With

I'm not sure if the double use of Set otlAppetc is necessary or redundant, please feel free to edit this post if there is a more concise way to write it.

我不确定Set otlAppetc的双重使用是必要的还是多余的,如果有更简洁的写法,请随时编辑这篇文章。

回答by Chuck Trese

@JoeMalpass,

@乔马尔帕斯,

I know this is an old post, but for the benefit of anyone else who might stumble on this...

我知道这是一个旧帖子,但为了其他可能偶然发现此问题的人的利益......

From reading your solution, I learned that the problem is that when you use .Display on an email created from a template, Outlook appends a signature. Since that is the only problem, it occurred to me that the solution might be much simpler than the one you presented.

通过阅读您的解决方案,我了解到问题在于当您在从模板创建的电子邮件上使用 .Display 时,Outlook 会附加一个签名。由于这是唯一的问题,我想到该解决方案可能比您提供的解决方案简单得多。

In the code from your original post, you can simply replace

在原始帖子的代码中,您可以简单地替换

    .Display

with

    BodyWithoutSignature = .HTMLBody
    .Display
    .HTMLBody = BodyWithoutSignature

Since .Display appends a signature you don't want, simply replace the body with what was there before the signature was added. I tested it, and (was surprised that) it actually works. It was that easy!

由于 .Display 附加了您不想要的签名,只需将正文替换为添加签名之前的内容即可。我测试了它,并且(很惊讶)它确实有效。就这么简单!