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

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

How to call a macro on send button click?

vbaoutlook-vba

提问by M?nica

I have a macro that takes the description of an email that I've selected and populates the "message" field of a form that is created from a template:

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

sText = olItem.Body

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

In this template, I have more fields to fill, like combobox.. for example.

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

I would like to know, how do I get the value of this combo when I click on the send button, and concatenate it to the contents of the email before sending?

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

Generating something like this:

生成这样的东西:

EmailDesc: TEST SEND EMAIL BLA BLA BLA..
ComboboxValue: Item1

Thx

谢谢

回答by Kazimierz Jawor

You need to use Application_ItemSend eventwhich fires when you press send button. You create this event in ThisOutlookSession module. Your event sub could look like this:

Application_ItemSend event当您按下发送按钮时,您需要使用which 触发。您在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

Be careful- this will make action to each of your e-mail therefore you should add some if statementsto make it works only with some of your e-mails.

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