使用 VBA 在电子邮件正文中插入文本

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

Inserting text in Email body using VBA

excel-vbaoutlook-vbavbaexcel

提问by user2862496

I am using the following VBA macro to mail a range of cells from Excel

我正在使用以下 VBA 宏从 Excel 邮寄一系列单元格

ActiveSheet.Range("A2:Q133").Select
ActiveWorkbook.EnvelopeVisible = True

   With ActiveSheet.MailEnvelope
    .Introduction = "demo"
      .Item.To = "[email protected]"
      .Item.Subject = "Testing mail"
      .Item.Send
   End With 

I want to modify the code to insert some text in the end. I tried using

我想修改代码以在最后插入一些文本。我尝试使用

.Item.HTMLBody = "Random text"

but the mail doesnt contain the text.

但邮件不包含文本。

Please let me know if i am doing anything wrong and what is the correct way to do it. Thanks in advance!

如果我做错了什么,请告诉我,正确的做法是什么。提前致谢!

回答by mucio

With MailEnvelopeyou send the whole sheet as body of your email.

随着MailEnvelope您发送整个板作为电子邮件的身体。

Introduction allows you to add some text before a mail item, but you have no other way to add something at the end than append something to the Excel sheet.

简介允许您在邮件项目之前添加一些文本,但除了将某些内容附加到 Excel 工作表之外,您没有其他方法可以在末尾添加某些内容。

Even if you modify the Bodyor the HTMLBody, this doesn't have effect on the email.

即使您修改了BodyHTMLBody,这也不会对电子邮件产生影响。

Maybe you could try to add same the message as draft and reopen it as a different object to modify it, but it seems over complicated. Much easier just to add some temporary text at the end of the excel spreasheet and remove it after .Item.Send

也许您可以尝试添加与草稿相同的消息,然后将其作为不同的对象重新打开以进行修改,但这似乎过于复杂。只需在excel电子表格的末尾添加一些临时文本并在之后将其删除就容易多了.Item.Send