Run a macro when new message form opens

  • Thread starter mkboynton via OfficeKB.com
  • Start date
M

mkboynton via OfficeKB.com

Is there anyway to make this run when the new mail message form loads?

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim myOlApp As New Outlook.Application
Dim myOLItem As Outlook.MailItem
Dim OrigBody
Set myOLItem = myOlApp.ActiveInspector.CurrentItem

If myOLItem.Subject = "FL DAILY INBOUND SHEET.xls" Then
OrigBody = myOLItem.HTMLBody
myOLItem.HTMLBody = "<HTML><H3>" & Format(Date, "MMM. DD, YYYY") &
"</H3></HTML>" & OrgBody
End If

If myOLItem.Subject = "Dropped Trailer Report Updates.xls" Then
OrigBody = myOLItem.HTMLBody
myOLItem.HTMLBody = "<HTML><H3>" & Format(Date, "MMM. DD, YYYY") &
"</H3></HTML>" & OrgBody
End If

End Sub

Like this, it only runs when the message is sent.
 
K

Ken Slovak - [MVP - Outlook]

Set up to handle NewInspector of the Inspectors collection. In that event if
it's a mail item instantiate an Inspector object declared WithEvents. Handle
whatever events you want that are fired by that Inspector, such as Activate
or Send.

If this is Outlook VBA code never, ever use New Outlook.Application, that
produces untrusted code. Use the intrinsic Application object, which is
trusted, and derive all your other Outlook objects from Application.
 
M

mkboynton via OfficeKB.com

Were would code for the NewInspector go?
Set up to handle NewInspector of the Inspectors collection. In that event if
it's a mail item instantiate an Inspector object declared WithEvents. Handle
whatever events you want that are fired by that Inspector, such as Activate
or Send.

If this is Outlook VBA code never, ever use New Outlook.Application, that
produces untrusted code. Use the intrinsic Application object, which is
trusted, and derive all your other Outlook objects from Application.
Is there anyway to make this run when the new mail message form loads?
[quoted text clipped - 19 lines]
Like this, it only runs when the message is sent.
 
K

Ken Slovak - [MVP - Outlook]

Everything would be in the ThisOutlookSession class module if this was
Outlook VBA code. You can find examples of various event handlers in Outlook
VBA at www.outlookcode.com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top