2

I want to run a VBA function whenever someone creates a new email in Outlook. This could happen either because they click on the 'New Mail' button, or because a third-party program generates a new mail window.

I have a third-party application that launches a new email (opens a window for composing an email). My firm wants the font automatically changed to Calibri.

I tried Application_ItemLoad() and inspecting the Item object that is passed as an event argument.

In 2007 I get

"The item's properties and methods cannot be used inside this event procedure."

Apparently this is a known issue in Outlook 2007.

Community
  • 1
  • 1
Steve Gore
  • 356
  • 6
  • 12
  • 1
    See the NewInspector event example here http://stackoverflow.com/questions/3674832/can-i-fire-an-outlook-2003-macro-when-the-user-creates-a-new-blank-message/3680546#3680546 – niton Oct 26 '13 at 23:35

3 Answers3

2

In Outlook 2010, it worked for me and I didn't find bug reporting. What is your exact code? What do you mean, "inspect the item object"?

You just put your code in the event like this:

Private Sub Application_ItemLoad(ByVal Item As Object)
    MsgBox "New mail item."
End Sub

That's all.

I hope that these remarks provided by MS my be useful to you: http://msdn.microsoft.com/en-us/library/office/ff868544.aspx

Remarks:

This event occurs when the Outlook item begins to load into memory. Data for the item is not yet available, other than the values for the Class and MessageClass properties of the Outlook item, so an error occurs when calling any property other than Class or MessageClass for the Outlook item returned in Item. Similarly, an error occurs if you attempt to call any method from the Outlook item, or if you call the GetObjectReference method of the Application object on the Outlook item returned in Item.

Edit:
The best I could come up with is by putting this code into the Application_ItemSend event method:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim myInspector
Dim wdDoc
Dim rng

Set myInspector = Item.GetInspector
Set wdDoc = myInspector.WordEditor

Set rng = wdDoc.Application.Selection

With rng
    With rng.Style.Font
        .Name = "Arial Black"
        .Size = 12
    End With
End With

Set myInspector = Nothing
Set wdDoc = Nothing

End Sub

The problem is that you cannot set properties on an item that is not yet available yet (as described by MS). Well, from this point of view it is actually impossible.

html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • I have a third-party application that launches a new email (as in, opens up a window for composing an email). However, it does it in a font that my firm doesn't like. They want me to make it so that when the email window opens, the font is automatically changed to Calibri. – Steve Gore Oct 24 '12 at 09:26
  • Yeah, that's the wall I keep hitting up against as well. Thanks for the confirmation. – Steve Gore Oct 25 '12 at 23:05
0

I came up with a way to do this for replies. I have not yet figured out how to do this for new compositions though.

Public WithEvents myExplorer As Outlook.Explorer 'Gives us the ability to trigger off explorer events
Public WithEvents myMailItem As Outlook.MailItem 'Gives us the ability to trigger off MailItem events

Private Sub Application_Startup()   
    Set myExplorer = Application.ActiveExplorer 'Initialize
End Sub

Private Sub myExplorer_SelectionChange()
    If (myExplorer.Selection.Count > 0) Then
        If (myExplorer.Selection.Item(1).Class = olMail) Then
            Set myMailItem = myExplorer.Selection.Item(1) 'Initialize
        End If
    End If
End Sub


Public Sub NewHTMLEmail(htmlToUse As String, Addressee As String, SubjectLine As String)
    Dim NewEmail As Outlook.MailItem
    Set NewEmail = Application.CreateItem(olMailItem)
    NewEmail.BodyFormat = olFormatHTML
    NewEmail.Subject = SubjectLine
    NewEmail.To = Addressee
    NewEmail.HTMLBody = htmlToUse
    NewEmail.Display
End Sub

'This trigger executes when we hit the "Reply" button while viewing a mail item
Private Sub myMailItem_Reply(ByVal Response As Object, Cancel As Boolean)
    Dim htmlString As String, thisMailItem As Outlook.MailItem, MissingText As String
    Set thisMailItem = Response
    Call NewHTMLEmail(thisMailItem.HTMLBody, thisMailItem.To, thisMailItem.Subject)

    Response.Close olDiscard
    thisMailItem.Delete    
End Sub
0

Looks like this answer may do the trick: outlook event newMail (newItem)

This sample code should be placed in the ThisOutlookSession module. Restart Outlook and whenever you create a new email you will see the message box.

Private WithEvents objinspectors As Outlook.Inspectors

Private Sub Application_Startup()
  Set objinspectors = Application.Inspectors
End Sub

Private Sub objinspectors_NewInspector(ByVal Inspector As Inspector)
  If TypeName(Inspector.CurrentItem) = "MailItem" Then
    MsgBox "newinspector"
  End If
End Sub
Jedi-X
  • 21
  • 6