Outlook 2007 Rules & VBA: How to run a script on a report message (ReportItem)

Status
Not open for further replies.

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Hi Outlook VBA Experts: I apologize for everything being in one sentence, but I am unable to get the forum to recognize line breaks. I posted a separate question about this, with no resolution so far, so I am going ahead with my real purpose for being on the forum. 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 (the BB code tags work, but the four lines of code are run together...but I'm sure you can parse it...I've put in some extra spaces that will help):

Code:
Sub Display     Subject (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 I'm a newbie to Outlook VBA (in fact, to all forms of VB). Thanks very much, Joe P.S. I apologize if this has already been addressed, but my searches in the forums turned up no hits.
 

Forum Admin

Senior Member
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

At least it's a short macro. :)

This tells it to look for mailitems - MyMail As MailItem - try changing mailitem to object and see if it works.
 

Forum Admin

Senior Member
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

At least it's a short macro. :)




This tells it to look for mailitems - MyMail As MailItem - try changing mailitem to object and see if it works.
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

Earlier today I tried it As Object and As Outlook.ReportItem and As ReportItem. No joy in all three cases. The macro doesn't even show up in the list of available scripts when I click "run a script" in the rule.
 

Forum Admin

Senior Member
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

oh, yeah, run a script only works with mailitems. If you use an itemadd macro, you can look for report items.
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

OK, now I'll have to go back to my earlier comment about being a newbie to this world. I don't have a clue what an "itemadd macro" is. What code do I need to put into ThisOutlookSession (or does it go somewhere else?) and what does the Outlook rule need to be in order for every message received (both normal emails and report emails) to run some code that has access to the subject, sender, and body of the message? For starters, let's just say that it executes {MsgBox strSubject & vbNewLine & strSender} with appropriate Dim statements for the variables. Thanks!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

This will watch the inbox for new items and when one is found, do something.




Option Explicit


Private WithEvents Items As Outlook.Items



Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items


End Sub



Private Sub Items_ItemAdd(ByVal Item As Object)
' do whatever


End Sub
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

Hi Diane,

I copied/pasted your code above into ThisOutlookSession. I changed the "do whatever" comment to a MsgBox. I then went to create a rule and it doesn't show up when I select "run a script". So I removed it from ThisOutlookSession and put it in a Module, thinking it would show up as a Macro when I do Alt-F8 - it doesn't. Where do I need to put that code? How do I test it on items already in a folder (as you can with a rule)? How do I set it to execute on new items that arrive (i.e., after testing and I want it to run "for real")? Thanks, Joe
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

This is an item add macro - it doesn't run in a rule, it watches a folder (inbox in this example) and does something when a new item is added to the folder.

The application startup macro starts it when outlook loads. You can kick start it during testing (without restarting Outlook) by clicking in the application startup macro and pressing the Run button or F5. If it errors or you make changes, you'll need to repeat the Run command. Otherwise, it will run as long as outlook is open.

If you need to filter mail and apply the rule to certain items, you can use If statements.
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Re: Outlook 2007 Rules & VBA: How to run a script on a report message (ReportI

Hi Diane,

I pasted your code into ThisOutlookSession (with a simple MsgBox statement so I would know it worked), but Macros (Alt-F8) shows an empty window. I ran it anyway (Run/F5) by clicking in the Application Startup macro in ThisOutlookSession, but it didn't fire when a message came in. To cover all the bases, I then closed Outlook and restarted it. Same thing - no MsgBox. Even clicked Run/F5 again - no MsgBox. Here's what I pasted into ThisOutlookSession:

Code:
Option Explicit 
 
Private WithEvents Items As Outlook.Items 
 
Private Sub Application_Startup() 
 
Dim Ns As Outlook.NameSpace 
 
Set Ns = Application.GetNamespace("MAPI") 
 
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items 
 
End Sub 
 
Private Sub Items_ItemAdd(ByVal Item As Object) 
 
MsgBox "Diane code worked" 
 
End Sub
Regards, Joe
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Re: Outlook 2007 Rules & VBA: How to run a script on a report message

It's not going to show in the macros window - the part in the () means it's can only be called from another macro. In this code, it's triggered when a new message is added to the inbox. You need to click in the Application startup macro so it watches the inbox, then send a message to yourself. When its downloaded the itemadd macro kicks off.
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Hi Diane,
I apologize profusely for going silent on you. I truly appreciate your efforts to help me and feel extremely guilty about not being a responsive asker. I am usually on the other end of this, as I participate in several forums where I am often the answerer, not the asker, and I really dislike it when an asker abandons a question. So I am very sorry for doing it to you on this issue, but I am now in a position to pick up the ball on it. I will try your most recent suggestion today and promise to be responsive until we have a solution. Thank you for your understanding. Regards, Joe
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
15 days is not an abandonment. :) We all get busy at times and can't get back immediately - i do it myself at times.
 

JoeWinograd

Member
Outlook version
Outlook 2007
Email Account
IMAP
Hi Diane,

Still not working. When I copy/paste into ThisOutlookSession, it puts these lines in (General)/(Declarations):

Option Explicit
Private WithEvents Items As Outlook.Items

It puts these lines in Application/Startup:

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderInbox).Items
End Sub

It puts these lines in Items/ItemAdd:

Private Sub Items_ItemAdd(ByVal Item As Object)
MsgBox "Diane code worked"
End Sub

There doesn't seem to be any way to force all of the code into Application/Startup. I don't know if that's the problem, but the bottom line is that I'm not getting the MsgBox when a message arrives in the Inbox. Speaking of the Inbox, how does it know which Inbox to watch (when you have more than one Account/Inbox) or does it watch all of them?

Update: One other point. I know that the Startup code is being executed, because I added a MsgBox statement after the "Set Items" line and I do get the MsgBox when Outlook starts. Thanks, Joe
 
Status
Not open for further replies.
Top