如何使用 VBA 在 Outlook 电子邮件编辑器中插入文本 [英] How to insert text into Outlook email editor using VBA

查看:63
本文介绍了如何使用 VBA 在 Outlook 电子邮件编辑器中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所需的工作流程:

  • 点击回复电子邮件
  • 在电子邮件编辑器中,将文本光标放在电子邮件正文中的所需位置以插入文本
  • 执行宏(通过单击电子邮件编辑器窗口快速访问工具栏中的图标).宏将插入日期.

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

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

预先感谢您的帮助.

解决方案


参考 Microsoft Word xx.x 对象库


<块引用>

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

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

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

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

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.

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.

Thank you in advance for your help.

解决方案

InsertBefore Method or InsertAfter Method

Inspector.WordEditor Property (Outlook)

Application.ActiveInspector Method (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



Reference to Microsoft Word xx.x Object Library


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.

  • 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".

这篇关于如何使用 VBA 在 Outlook 电子邮件编辑器中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆