Zikato
Zikato

Reputation: 605

How to call Outlook's Desktop Alert from VBA Code

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

Upvotes: 7

Views: 5147

Answers (3)

FreeSoftwareServers
FreeSoftwareServers

Reputation: 2791

I was able to get this to work by simply adding a sleep to my code before moving item.

See --> https://stackoverflow.com/a/68959008/5079799

Eg:

#If VBA7 Then
'Code is running VBA7 (2010 or later).

     #If Win64 Then
     'Code is running in 64-bit version of Microsoft Office.
      Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
     #Else
     'Code is running in 32-bit version of Microsoft Office.
      Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
     #End If

#Else
'Code is running VBA6 (2007 or earlier).

#End If

Public Sub MoveOLItem(objMail As Outlook.MailItem, FolerPathStr As String)
 If FolerPathStr <> "" Then
  Dim olFolder As Outlook.Folder
  Set olFolder = GetFolder(FolerPathStr)
  Sleep 4200
  objMail.Move olFolder
  Debug.Print "Moved Item to " & FolerPathStr
 ElseIf FolerPathStr = "" Then
  Debug.Print "FolderPathStr = ''"
 End If
End Sub

Upvotes: 0

Eric Vaughn-Shobey
Eric Vaughn-Shobey

Reputation: 131

Found this to set the Rule to work as needed without VBA so that when your macro sorts, alerts are displayed based on the folder to which you moved the incoming email.

https://www.howto-outlook.com/howto/newmailalert.htm

Scroll down to "Configure the mail alert to monitor al[l] folders; not just the inbox."

Configure the Mail Alert to monitor al folders; not just the Inbox Manage Rules & Alerts buttonBy default the new New Mail Desktop Alert will only show when the mail is delivered to the Inbox. This means that when you have a rule configured to move your mail to a different folder the Notification won’t show.

To workaround this, you can add the action “display a Desktop Alert” to each and every rule. Besides the fact that it is very tiresome, the real downside of this is that when you are in an Exchange organization the rule will become a local rule so that it will only execute when Outlook is running. This means that when you have added extra actions to the rule, like forwarding it to another address, those actions won’t be executed either.

A better solution is to create a generic rule with no conditions and just the action to display the Desktop Alert.

Open the Rules and Alerts dialog; Outlook 2007 Tools-> Rules and Alerts… Outlook 2010, Outlook 2013 and Outlook 2016 File-> button: Manage Rules & Alerts Button New Rule… Select “Start from a blank rule” and verify that “Check messages when they arrive” or “Apply rule on message I receive” is selected. Press Next to go to the Conditions screen. Verify that no condition is selected and press Next. A warning will pop-up stating that this rule will apply to all messages. Press “Yes” to indicate that that is correct. Select the action “display a Desktop Alert”. Press Finish to complete the rule. If needed move the “display a Desktop Alert” rule all the way to the top. New Mail Desktop Alert rule (click on image to enlarge) You can create a rule to show the New Mail Desktop Alert for each message that you receive.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

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.

Upvotes: 1

Related Questions