vba - 如何在单击发送按钮时调用宏?

标签 vba outlook

我有一个宏,它接受我选择的电子邮件的描述,并填充从模板创建的表单的“消息”字段:

sText = olItem.Body

Set msg = Application.CreateItemFromTemplate("C:\template.oft")
With msg
   .Subject = "Test"
   .To = "user@user.com"
   'Set body format to HTML
   .BodyFormat = Outlook.OlBodyFormat.olFormatHTML
   .HTMLBody = "<HTML><BODY>EmailDesc: " + sText + "</BODY></HTML>"
   .Display
End With

在这个模板中,我有更多的字段需要填写,比如组合框......例如。

我想知道,当我点击发送按钮时,我如何获得这个组合的值,并在发送前将其连接到电子邮件的内容?

生成这样的东西:
EmailDesc: TEST SEND EMAIL BLA BLA BLA..
ComboboxValue: Item1

谢谢

最佳答案

您需要使用 Application_ItemSend event当您按下发送按钮时会触发。您在 ThisOutlookSession module 中创建此事件.您的事件子可能如下所示:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error GoTo ErrorHandler

    With Item   'Item is your e-mail
        'this way you could change your subject just before you send message
        .Subject = "test subject"   
        'here some changes regarding body of the message
        .Body = .Body & " Additional text at the end or " & _
                "ComboBoxValue: " '& ... reference to combobox value here
    End With

Exit Sub
ErrorHandler:
    MsgBox "Error!"
End Sub

小心 - 这将对您的每封电子邮件进行操作,因此您应该添加一些 if statements使其仅适用于您的某些电子邮件。

关于vba - 如何在单击发送按钮时调用宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17569372/

相关文章:

关闭Excel时VBA密码提示

vba - 搜索邮政编码范围,拉取对应的Zone

excel - 为什么 Excel VBA 中的银行家舍入函数会根据小数位数给出不同的结果?

html - 无法获得适用于 MS Outlook 的 css 宽度

outlook - 使用iCalendar文件导入删除日历事件(Outlook 2003问题)?

VBA方法 'range of object'_运行代码时工作表突然出现失败?

VBA - 将表行复制并粘贴到另一个表

outlook - MS 访问引用

vba - 如何使用 VBA 将 Word 文档作为电子邮件正文发送

vba - Outlook 2013 使用 VBA 发送草稿