Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
933 views
in Technique[技术] by (71.8m points)

vba - How to call a macro on send button click?

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:emplate.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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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 statements to make it works only with some of your e-mails.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...