Outlook Macro to popup a box if a mail is receive on a particular folder...

Hi,

Outlook Macro to popup a box if a new mail is receive on a particular folder...

Is there a way to receive a popup on any mail that come in to a folder...

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
 
David LeeCommented:
Ok.

Private WithEvents olkInbox As Outlook.Items

Private Sub Application_MAPILogonComplete()
    'Change the path on the following line to that of the folder you want to monitor
    Set olkInbox = OpenOutlookFolder("Folder\SubFolder").Inbox
End Sub

Private Sub Application_Quit()
    Set olkInbox = Nothing
End Sub

Private Sub olkInbox_ItemAdd(ByVal Item As Object)
    'Set the message on the following line as desired.
    MsgBox "Your message goes here."
End Sub

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        olkFolder As Outlook.MAPIFolder
    On Error GoTo ehOpenOutlookFolder
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        If Left(strFolderPath, 1) = "\" Then
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        End If
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkFolder) Then
                Set olkFolder = Session.Folders(varFolder)
            Else
                Set olkFolder = olkFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkFolder
    End If
    On Error GoTo 0
    Exit Function
ehOpenOutlookFolder:
    Set OpenOutlookFolder = Nothing
    On Error GoTo 0
End Function

Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
0
 
David LeeCommented:
Hi, bsharath.

The answer depends on what type of box you have in mind.  Yes, it is possible to monitor a particualr folder and pop ip a dialog-box when an item is added to that folder.  But, that dialog-box won't be the same new message box that Outlook displays when an item is delivered.  And this only works for the full version of Outlook, not Outlook Express.  If you're interested, then I can post the code and instructions for using it.
0
 
bsharathAuthor Commented:
Hi,

I want it for Misrosoft Outlook ver 2007.I want a popup saying i have a new mail...
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
bsharathAuthor Commented:
Just wanted to know if you can help with this post...
http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22864767.html
0
 
David LeeCommented:
I apologize for being slow.  Here's the code for doing this.  Follow these instructions to use it.

1.  Start Outlook.
2.  Click Tools->Macros->Visual Basic Editor.
3.  If not already expanded, expand Microsoft Office Outlook Objects and select ThisOutlookSession.
4.  Copy the code and paste it into the right-hand pane of the VB editor.
5.  Edit the code as desired.
6.  Click the diskette icon on the toolbar to save the code.
7.  Close the VB editor.
8  Follow these instructions to sign the macro: http://office.microsoft.com/en-us/help/HA012317811033.aspx
9.  Click Tools->Trust Center
10. Click Macro Security.
11. Ensure that Macro Security is set to "Warnings for signed macros; all unsigned macros are disabled"
12. Close Outlook.
13.  Start Outlook.

I want t remind you that the popup generated by this will NOT work like the notification messages generated by Outlook.  This will display a standard Windows dialog-box that will remian onscreen until you click a button.  The dialog-box will block other Outlook actions until you click its button.

Private WithEvents olkInbox As Outlook.Items

Private Sub Application_MAPILogonComplete()
    'Change the path on the following line to that of the folder you want to monitor
    Set olkInbox = OpenOutlookFolder("Folder\SubFolder").Inbox
End Sub

Private Sub Application_Quit()
    Set olkInbox = Nothing
End Sub

Private Sub olkInbox_ItemAdd(ByVal Item As Object)
    'Set the message on the following line as desired.
    MsgBox "Your message goes here."
End Sub

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
    Dim arrFolders As Variant, _
        varFolder As Variant, _
        olkFolder As Outlook.MAPIFolder
    On Error GoTo ehOpenOutlookFolder
    If strFolderPath = "" Then
        Set OpenOutlookFolder = Nothing
    Else
        If Left(strFolderPath, 1) = "\" Then
            strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
        End If
        arrFolders = Split(strFolderPath, "\")
        For Each varFolder In arrFolders
            If IsNothing(olkFolder) Then
                Set olkFolder = Session.Folders(varFolder)
            Else
                Set olkFolder = olkFolder.Folders(varFolder)
            End If
        Next
        Set OpenOutlookFolder = olkFolder
    End If
    On Error GoTo 0
    Exit Function
ehOpenOutlookFolder:
    Set OpenOutlookFolder = Nothing
    On Error GoTo 0
End Function
0
 
bsharathAuthor Commented:
Hi,

I need to change just these lines right

Set olkInbox = OpenOutlookFolder("Sharath").Inbox

    MsgBox "Your message goes here."

I have a folder called sharath.This has a rule that when a mail is sent to my self it comes to the folder.
Sharath is right below the Inbox.

Is the change correct.
I just tried but did not receive a popup.
0
 
David LeeCommented:
The change is close, but not quite right.  Paths in Outlook are just like paths in the file system, except they don't involve drive letters.  The path to the folder you described is probably

    Mailbox - Your Mailbox Name\Inbox\Sharath

Also, I had a typo in my code.  The line should have read

    Set olkInbox = OpenOutlookFolder("Folder\SubFolder").Items

0
 
bsharathAuthor Commented:
Hi,

As the folder "Sharath" is in my Pst below the inbox..
Should i mention the Pstname\Inbox\sharath

Set olkInbox = OpenOutlookFolder("Pstname\Inbox\sharath").Items
0
 
David LeeCommented:
Yes.
0
 
bsharathAuthor Commented:
I get a  error here.


---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Sub or Function not defined
---------------------------
OK   Help  
---------------------------


Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder

This line looks like this...

Set olkInbox = OpenOutlookFolder("personal folder\inbox\Sharath").Item
0
 
David LeeCommented:
This line

    Set olkInbox = OpenOutlookFolder("personal folder\inbox\Sharath").Item

should end with "Items", plural, not "Item".
0
 
bsharathAuthor Commented:
I get this.

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Sub or Function not defined
---------------------------
OK   Help  
---------------------------

This line is highlitted.

Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIFolder
0
 
David LeeCommented:
Oops.  Looks like I missed one function when I pasted the code in.  Apologies!  Here it is.

Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
0
 
bsharathAuthor Commented:
Hi,

Can i have the full code here please...
0
 
bsharathAuthor Commented:
Hi still does not popup.

I get this line in Red..
Private WithEvents olkInbox As Outlook.Items
0
 
David LeeCommented:
Is that line at the very top of the module, or is there code above it?  Is it in the correct module, ThisOutlookSession?  Did you insert the path to your folder?  
0
 
bsharathAuthor Commented:
Hi,

I have tried mentioning them path like this.
   Set olkInbox = OpenOutlookFolder("inbox\sharath").Inbox
like this.
   Set olkInbox = OpenOutlookFolder("personal folder\inbox\sharath").Inbox

Personal folder is the profile name
sharath is the outlook folder name

Is there any thing that i am doing wrong
0
 
David LeeCommented:
Inbox was a typo that I corrected several posts back.  It should be Items.
0
 
bsharathAuthor Commented:
Hi i tried this but still dont get a popup...
0
 
David LeeCommented:
I know the code works, I've tested it here, so there has to be something going on at your end.  Are you certain of the folder path?  That's the most likely cause I can think of.  Beyond that, please post the code you are using now so I can verify it.
0
 
bsharathAuthor Commented:
Hi,

When i go to properties of the folder"Sharath"
The location looks like this.

\\Personal Folders\Inbox

I even tried giving \\personal folder\inbox\sharath
still dont get an error or popup.
Is there any reference that i need to enable

When i try to play the macro i get this.

---------------------------
Microsoft Visual Basic
---------------------------
The macros in this project are disabled.  Please refer to the online help or documentation of the host application to determine how to enable macros.
---------------------------
OK   Help  
---------------------------
But the " No warning check for macro" is enabled.
0
 
David LeeCommented:
The first time a macro is run in Outlook you should get the message about macros being disabled, but you should be asked if you want to enable them.  I take it that isn't happening, right?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.