如何使用 VBA 在 Outlook 电子邮件编辑器中插入文本

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

How to insert text into Outlook email editor using VBA

vbaemailoutlookms-wordoutlook-vba

提问by Marty131

I wish to create a macro that inserts the date into an email body which is currently open for editing, prior to sending.

我希望创建一个宏,在发送之前将日期插入当前打开进行编辑的电子邮件正文中。

I am using Outlook 2013 on a Windows 7 machine.

我在 Windows 7 机器上使用 Outlook 2013。

Desired workflow:

所需的工作流程:

  • Click reply to an email
  • Within the email editor, place the text cursor within email body for desired position to insert text
  • Execute the macro (by clicking on an icon in the email editor window's quick access toolbar). Macro will insert the date.
  • 单击回复电子邮件
  • 在电子邮件编辑器中,将文本光标放在电子邮件正文中的所需位置以插入文本
  • 执行宏(通过单击电子邮件编辑器窗口快速访问工具栏中的图标)。宏将插入日期。

(Assume the date has been stored as a string variable, i.e. the macro simply inserts a variable into the email body at a desired position).

(假设日期已存储为字符串变量,即宏只是在电子邮件正文的所需位置插入一个变量)。

All Outlook VBA examples I have found for inserting text into an email body involve storing the active email's HTMLBody as a string, appending the desired text to that string, then creating a brand new email, and re-populating the to, cc, bcc, subject and htmlbody. I wish to avoid this, as it seems very clunky.

我发现的所有用于将文本插入电子邮件正文的 Outlook VBA 示例都涉及将活动电子邮件的 HTMLBody 存储为字符串,将所需文本附加到该字符串,然后创建一个全新的电子邮件,并重新填充 to、cc、bcc、主题和 htmlbody。我希望避免这种情况,因为它看起来很笨重。

Thank you in advance for your help.

预先感谢您的帮助。

回答by 0m3r

InsertBefore Methodor InsertAfter Method

InsertBefore 方法InsertAfter 方法

Inspector.WordEditor Property (Outlook)

Inspector.WordEditor 属性 (Outlook)

Application.ActiveInspector Method (Outlook)

Application.ActiveInspector 方法 (Outlook)

Example

例子

Option Explicit
Public Sub Example()
    Dim Inspector As Outlook.Inspector
    Dim wdDoc As Word.Document
    Dim Selection As Word.Selection


    Set Inspector = Application.ActiveInspector()
    Set wdDoc = Inspector.WordEditor
    Set Selection = wdDoc.Application.Selection
        Selection.InsertBefore Format(Now, "DD/MM/YYYY")


    Set Inspector = Nothing
    Set wdDoc = Nothing
    Set Selection = Nothing
End Sub


enter image description here

在此处输入图片说明



Reference to Microsoft Word xx.x Object Library

参考 Microsoft Word xx.x 对象库



Go to Outlook VBA editor either by pressing "Alt + F11"keys or clicking on the "Visual Basic"button in the “Developer” ribbon.

  • 1.In VBA editor window, click the "Tools" button in the menu bar.

  • 2.Then, from the drop down list, select the "References" option.

enter image description here

  • 3.In the dialog box, you can pull the scrolling bar down until you locate what you want, such as "Microsoft Word XX.X Object Library".

enter image description here

通过"Alt + F11"按键或单击"Visual Basic"“开发人员”功能区中的按钮转到 Outlook VBA 编辑器。

  • 1.在 VBA 编辑器窗口中,单击菜单栏中的“工具”按钮。

  • 2.然后,从下拉列表中选择“参考”选项。

在此处输入图片说明

  • 3.在对话框中,您可以向下拉滚动条,直到找到您想要的内容,例如“Microsoft Word XX.X Object Library”。

在此处输入图片说明