• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1362
  • Last Modified:

Outlook 2007 Rules and VBA - How to run a script on a ReportItem

In Outlook 2007, I have a VBA script that is used in a rule that runs on all messages when they arrive. It works fine on normal messages (MailItem), but is not even invoked on a report message (ReportItem). For example, let's say this is the script:

Sub DisplaySubject (MyMail as MailItem)
Dim strSubject = MyMail.Subject
MsgBox strSubject
End Sub

And let's say the only action in the rule is "run a script" and it is the script above. Problem is, it doesn't run on report messages. How would I get a script like the above to run on report items as well as mail items? Please provide detailed instructions/code, as my Outlook VBA skills (in fact, all forms of VB) are modest. Thanks very much, Joe
0
Joe Winograd, Fellow&MVE
Asked:
Joe Winograd, Fellow&MVE
  • 9
  • 7
2 Solutions
 
Rgonzo1971Commented:
Hi,

pls try
Sub RunDisplaSubject(ByVal myItem As Object)
    On Error Resume Next
    If TypeName(myItem) = "MailItem" Or TypeName(myItem) = "ReportItem" Then
	On Error goto 0
        DisplaySubject myItem
    End If
    On Error goto 0
End Sub

Sub DisplaySubject (MyMail as Object)
    Dim strSubject = MyMail.Subject
    MsgBox strSubject
End Sub

Open in new window

Or
Sub DisplaySubject (MyItem as Object)
    On Error Resume Next
    If TypeName(myItem) = "MailItem" Or TypeName(myItem) = "ReportItem" Then
    Dim strSubject = MyItem.Subject
    MsgBox strSubject
    End If
    On Error goto 0

End Sub

Open in new window

Regards
0
 
David LeeCommented:
Hi, Joe.

I'm afraid this isn't possible in conjunction with a rule.  Rules do not fire for report items.  They only fire for messages and meeting requests.  There are other ways to accomplish this though that don't require a rule.  If you're interested in that kind of solution, then I can help.
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Hi Rgonzo1971,
Thanks for the ideas. Unfortunately, none of them produces a MsgBox when I create a rule with those scripts. I tried RunDisplaSubject (btw, did you really mean "Displa" or should it be "Display") and DisplaySubject with the first set of code, and DisplaySubject with the second set of code. After creating each rule (one at a time), I told it to run the script on messages already in the folder. None  of the scripts displayed a MsgBox. Also, both sets of code produce a red line of text...does that mean a problem? Here are screenshots:

Rgonzo first codeRgonzo second codeDoes any of this code work on your computer? Thanks, Joe
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Hi Lee,
I don't mind if it's not a rule...I'm interested in any solution that will write a plain text file with the EntryID, Sender, Subject, and Body for all normal messages and report messages that arrive. My current code that accesses MyMail.EntryID, MyMail.SenderEmailAddress, MyMail.Subject, and MyMail.Body (where MyMail As MailItem) works perfectly on normal messages but doesn't fire on report messages (the rest of the code simply writes a plain text file with a file name of the EntryID and a file type of TXT, such as 00000000B5C08539BEF98C42B99342E6CEA9BEFB84012000.txt). How can I accomplish this without a rule? Thanks much, Joe
0
 
David LeeCommented:
Joe,

Here's the code you need.  This code must go in the ThisOutlookSession module.  I'm not clear on whether you need the code that writes the data you want to a file, or if you just need the code that emulates a rule and captures both messages and reports.  The code below is the latter.  If you need me to add the code that writes the data to a file, then let me know and I'll add it.  If you already have that code, then insert it after my comment of "Your code goes here".

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim arrEID As Variant, _
        varEID As Variant, _
        olkItm As Object
    arrEID = Split(EntryIDCollection, ",")
    For Each varEID In arrEID
        Set olkItm = Session.GetItemFromID(varEID)
        Select Case olkItm.Class
            Case olMail, olReport
                'Your code goes here
        End Select
    Next
    Set olkItm = Nothing
End Sub

Open in new window

0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Hi Lee,
Here's my code snippet that writes the TXT file:

strID = MyMail.EntryID
strSender = MyMail.SenderEmailAddress
strSubject = MyMail.Subject
strBody = MyMail.Body
Set oShell = CreateObject("WScript.Shell")
strAppData = oShell.ExpandEnvironmentStrings("%APPDATA%")
strTXTfile = strAppData & "\mail\" & strID & ".txt"
Open strTXTfile For Append As 1
Print #1, strID
Print #1, strSender
Print #1, strSubject
Print #1, strBody
Close #1

Open in new window

Will this work as-is if inserted after "Your code goes here" comment or does it need changes? Thanks, Joe
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Lee,
Before even worrying about my last question, here's an issue. I simply put a MsgBox statement in place of your comment. I then cleared everything out of ThisOutlookSession and removed all of the other Modules, so the VB environment was clean. I then copied/pasted the code into ThisOutlookSession and went to create a rule using it. But it doesn't even show up when I click the "a script" phrase in the Rules Wizard — the Select Script window is empty. So I figured maybe it's a macro, but when I went to Tools>Macro>Macros (Alt-F8) the Macros window is also empty. How do I invoke your code? Thanks, Joe
0
 
David LeeCommented:
Joe,  

The code runs automatically.  You can't run it and as I'd already said, you cannot do this with a rule.  With the code in place, send yourself a message, or have someone send you a message.  That will trigger the code and display the message box you added.
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
> The code runs automatically.

Indeed, it does! I'm not able to test it right now on a report message, but it worked fine on a normal email. So then I replaced the MsgBox with my 13 lines of code above and VB gives an error dialog with:

Run-time error '424':

Object required

What's wrong with my code? Thanks, Joe
0
 
David LeeCommented:
You need to change MyMail to match the name of the object I used (i.e. olkItm), or change the name of my object (i.e. olkItm) to MyMail.  Either one will do.
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Ah, of course! Sorry for the newbie error. I'll try it in a moment with the fix.

Based on some research I've done, ReportItem does not have a SenderEmailAddress property (MailItem does). Do you know what will be returned in olkItm.SenderEmailAddress when a ReportItem is received? Also, so I can test code, how would I send a "report" email rather than a normal email? Thanks for all your help on this — I really appreciate it.
0
 
David LeeCommented:
Joe,

ReportItem objects don't have a SenderEmailAddress property.  The only solution I know of is to use Outlook's PropertyAccessor object to read the sender address from the unexposed properties of the ReportItem object.  To do that, add this code to what you already have.

Private Function GetSenderAddress(olkMsg As Object) As String
    Dim olkPA As Outlook.PropertyAccessor
    On Error Resume Next
    Set olkPA = olkMsg.PropertyAccessor
    GetSenderAddress = olkPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0C1F001E")
    On Error GoTo 0
    Set olkPA = Nothing
End Function

Open in new window


Once you've done that, change line #2 of your code from

strSender = MyMail.SenderEmailAddress

Open in new window


to

strSender = SenderEmailAddress(MyMail)

Open in new window


The only way I know of to get a ReportItem object is to send someone an email and request a read receipt.  When they read the message, Outlook will send you a receipt.  That receipt will be a ReportItem.
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Hi Lee,
I'm sure you meant:

strSender = GetSenderAddress(MyMail)

And after changing it to that, everything works a charm! Also, the read receipt idea works perfectly! Thanks for all of your excellent work on this...everything is hunky-dory now! Regards, Joe
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
Awesome solution...and he stuck with me persistently during some rookie errors! Very much appreciated!
0
 
David LeeCommented:
Yes, that's what I meant.  Sorry, sometimes my mind takes unscheduled vacations.  

Glad you like the solution.  Cheers!
0
 
Joe Winograd, Fellow&MVEDeveloperAuthor Commented:
>  sometimes my mind takes unscheduled vacations

I'll be stealing that phrase from you! :)
0
 
David LeeCommented:
LOL.  You're welcome to it.
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.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now