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.
Thread starter Similar threads Forum Replies Date
A outlook 2007 how to chcek all rules ? Outlook VBA and Custom Forms 2
M Outlook 2007 IMAP Account Rules Issues Using Outlook 3
I Outlook 2007 Rules Using Outlook 1
S Outlook 2007 Rules randomly stopping working (non-Exchange Server) Using Outlook 6
1 Rules: Outlook 2007 ignoring/not applying Rules Using Outlook 4
C Outlook 2007 Rules Using Outlook 1
S Upgraded from Office 2007 to Office 2010 and my Outlook rules have all disappeared Using Outlook 2
M Outlook 2007 rules error on text in subject Using Outlook 1
B Re: Outlook 2007 delete rules will not work Using Outlook 2
H How to transfer outlook express 2007 address book and rules between computers (not networked) Using Outlook 2
S In Outlook 2007 is there any way of organising rules? Using Outlook 3
S exchange 2007 Transport Rules not effective for outlook 2000, xp, 2003 Using Outlook 6
S Verwendung von Outlook 2007 Using Outlook 0
A Arthur needs help with 2007 Outlook e-mail Using Outlook.com accounts in Outlook 3
M PST import from Outlook 2007 to 2010 - Address Book contacts all in 1 group Using Outlook 4
S outlook 2007 calendar search Using Outlook 6
B Migrate Outlook 2007 to Office 365 Using Outlook 3
X I have met my waterloo trying to resolve embedded graphics problem with outlook 2007 and now 2016 Using Outlook 1
R Outlook 2007 only loads some appointments Using Outlook 0
C Move Outlook 2007 to new PC with Outlook 365 Using Outlook 3
J Outlook 2007 Hide Messages Option not Available Using Outlook 2
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 4
S Outlook 2007 Calendar instant search problem. Windows 7 Using Outlook 0
B Server errors Outlook 2007 Using Outlook 1
S Reboot of frozen windows7 results in changed outlook 2007 settings Using Outlook 1
S Outlook 2007 printing wrong email address at top of page Using Outlook 8
M Configure outlook 2007 to accept digital signatures Using Outlook 2
D Outlook 2007 crashes when opening an email Using Outlook 2
R New chap saying hello and needing advice on Outlook 2007 thumbnails Using Outlook 3
icacream From Outlook 2007 to 2016 ! Using Outlook 9
vodkasoda Object could not be found Error in Outlook 2007 Outlook VBA and Custom Forms 5
S Outlook 2007: Address Cards allow entering text! Why? Using Outlook 3
S View Appointment in Text Wrap in Outlook 2007 Month Calendar View Using Outlook 0
L Outlook 2007 Separate the Send/Receive functions Using Outlook 2
M Outlook 2007 Contacts Glitch: Creating a new email Using Outlook 1
C Move from Outlook 2007 Enterprise (MOE) to Outlook Pro plus 2007 Using Outlook 1
J reinstalling Outlook 2007 asking for user name & password Using Outlook 14
P outlook addin unloaded in office 2007 Using Outlook 0
B Fonts in Outlook 2007 Using Outlook 4
R Add Exchange Account to existing POP3 Outlook 2007 Profile Using Outlook 0
C out of space in file group Outlook 2007 Using Outlook 2
A Moving archived contents in Outlook 2007 back into working folders Using Outlook 0
P Outlook 2007 Email Categorization using VBA Outlook VBA and Custom Forms 1
M Unable to Configure Gmail Account in Outlook 2007 Using Outlook 1
R Outlook 2007 or 2010 - Lock Down Functionality Outlook VBA and Custom Forms 3
S Outlook 2007, windows 10 Font size Using Outlook 1
Diane Poremsky Manually create a POP3 account in Outlook 2007 New Slipstick.com Articles 0
J Can Click to Drag Custom Form Field But Cannot Drop When Designing in Outlook 2007 Outlook VBA and Custom Forms 2
L Outlook 2007 Font Using Outlook 3
J Outlook 2007 connector and Windows 10 Using Outlook 3
Similar threads


















































Top