Run a macro when new message form opens

  • Thread starter Thread starter mkboynton via OfficeKB.com
  • Start date 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.
 
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.
 
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.
 
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.
 
Back
Top