Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Outlook's Desktop Alert from VBA Code

Tags:

vba

outlook

I have Outlook 2003 running on Win XP. My Desktop Alert is turned on and running smoothly.

But recently I created a VBA Macro that sorts incoming emails into several different folders (via item_add event in ThisOutlookSession). This somehow stopped the desktop alert from showing.

Is there any way to call the Desktop Alert from the VBA code manually? Maybe a function of some sorts.

P.S: I can't sort my emails through rules, that's not an option

Basically I'm looking with RegEx for 6 digit code in email

My code (sorry, it's a bit of patchwork from other pieces of code I found on the Internet

Option Explicit

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next

Dim targetFolder As Outlook.MAPIFolder
Dim myName As String

Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match

Set Reg1 = New RegExp
myName = "[MyName]"

' \s* = invisible spaces
' \d* = match digits
' \w* = match alphanumeric

With Reg1
    .Pattern = "\d{6}"
    .Global = True
End With

If (InStr(Item.To, myName) Or InStr(Item.CC, myName)) Then    ' if mail is sent or cced to me
    If Reg1.test(Item.Subject) Then
        Set M1 = Reg1.Execute(Item.Subject)
        For Each M In M1
            ' M.SubMatches(1) is the (\w*) in the pattern
            ' use M.SubMatches(2) for the second one if you have two (\w*)
            Set targetFolder = GetFolder([folderPath])  ' function that returns targetFolder
            Exit For
        Next
    End If
    If Not targetFolder Is Nothing Then
        Item.Save
        Item.Move targetFolder
    End If
End If   
End Sub

Many thanks

like image 474
Zikato Avatar asked May 15 '15 11:05

Zikato


1 Answers

The Outlook object model doesn't provide anything for managing notifications. Instead, you may consider developing an add-in instead where you can use third-party components that allows to imitate the buit-in behavior. For example, take a look at the RadDesktopAlert component.

See Walkthrough: Creating Your First Application-Level Add-in for Outlook for more information.

like image 76
Eugene Astafiev Avatar answered Sep 27 '22 20:09

Eugene Astafiev