VB Outlook- Check Subject while writing

Viewed 29

I'm trying to create a macro in Outlook (thisOutlookSession) that checks the subject while I'm writing it, once I TAB and go to the email's body the macro has to check for subject and if subject is equal to a specific text then it opens a template. I've been able to write the part about the template, the difficult part is using the inspectors to check the subject while i'm writing it. Any solutions?

Forward the code:

    Private Sub subject()

    Dim subject As String
    Dim item As Outlook.MailItem
    Dim inspector As Outlook.inspector
    Dim template As Outlook.MailItem

    Set inspector = Outlook.ActiveInspector
    Set item = inspector.CurrentItem
    subject = item.subject

    Debug.Print subject

    If subject = "test" Then
    Set template = Application.CreateItemFromTemplate("C:test\test.oft")
    Display.template
    Else
    End If

    End Sub
2 Answers

The MailItem exposes aPropertyChange(String Name) event, which fires when the email subject field looses focus (among other).

You can hookup to it, but you need to declare the mail item WithEvents at module level.

See an example below:

Private WithEvents m_item As MailItem

Sub T()

    Set m_item = Application.CreateItem(olMailItem)
        m_item.Display
    
End Sub

Private Sub m_item_PropertyChange(ByVal Name As String)
    If Name = "Subject" Then Debug.Print m_item.Subject
End Sub

Please, try the next way:

  1. Create three variables on top of ThisOutlookSession:
   Private WithEvents m_Inspectors As Outlook.Inspectors
   Private WithEvents m_Inspector As Outlook.Inspector
   Private WithEvents myItem As Outlook.MailItem
  1. Copy the next Startup event code in ThisOutlookSession module:
 Private Sub Application_Startup()
     Set m_Inspectors = Application.Inspectors
 End Sub

Or copy only the line Set m_Inspectors = Application.Inspectors inside it, if already used for other purposes.

  1. Then, copy the next events code in the same module:
 Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then
       'Handle emails only:
       Set m_Inspector = Inspector
    End If
End Sub

 Private Sub m_Inspector_Activate()
    If TypeOf m_Inspector.CurrentItem Is MailItem Then 
       Set myItem = m_Inspector.CurrentItem '!!!
    End If
 End Sub
  1. And the PropertyChange event to be triggered when pressing enter after writing the subject (or clicking somewhere else: body, To, CC etc.):
 Private Sub myItem_PropertyChange(ByVal Name As String)
    Const specSubject As String = "mySubject..." 'use here the subject you need to open the template!
    Const templFullName As String = "C:test\test.oft"
    If Name = "Subject" Then
         If myItem.Subject = specSubject Then
                'do whatever you need...
                myItem.Close False 'probably you want closing the new Email. If not, comment this line...
                With Application.CreateItemFromTemplate(templFullName)
                    .Display
                End With
         End If
    End If
 End Sub

Now, manually press New Email button and play with the new mail window Subject...

Related