Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Counting messages


Reply
 
Thread Tools Display Modes
  #1  
Old 02-24-2003, 03:50 AM
florida florida is offline
Centurion
 
Join Date: Jan 2003
Posts: 190
Default Counting messages


Anyway to count how many times an Email is addressed to this name in my Outlook messages?

***All PeopleEmail

I need it to count how many messages in my Outlook 2000 mail were
addressed to: ***All PeopleEmail

I assume it would have search all the emails and get the
information from the "To" field.

From: Front Office

To: ***All PeopleEmail

CC:

Subject: Junk Data





Is this possible?
Reply With Quote
  #2  
Old 02-24-2003, 11:31 AM
Creeps Creeps is offline
Newcomer
 
Join Date: Feb 2003
Location: Nor-Cal
Posts: 21
Default

I believe this will help you out, however, I am sure that there is someone else that could do it in a graceful manner, instead of my hammering. Anyway here it is.

Code:
Public Sub CountMessages()
On Error GoTo ErrorHandler
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderSentMail)
Dim intCounter As Integer
Dim intLoop As Integer
intLoop = 0
Do
    intLoop = intLoop + 1
    Set myitem = myFolder.Items(intLoop)
    If myitem.To = "***All PeopleEmail" Then
        intCounter = intCounter + 1
    End If
Loop While myitem <> "Nothing"
Exit Sub
ErrorHandler:
    Select Case Err.Number    ' Evaluate error number.
        Case -2147352567    'Array index out of bounds
            Set myitem = Nothing
            MsgBox "***All PeopleEmail was emailed " & _ 
intCounter & " times."
        Case Else
            ' Handle other situations here...
    End Select
End Sub
This works in the Sent folder. You will have to change some of the code if you want it work in any other folder.
__________________
If you tell me how you get your feeling of importance, I'll tell you what you are.
Reply With Quote
  #3  
Old 02-24-2003, 12:41 PM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

Hammering away works just fine.

However, you could use the Find and FindNext methods
This example is written as a to run from inside of Outlook. If you need to run it from outside of Outlook there are plenty of examples in the forum (or just look at Creeps post).
Code:
Sub FlagMessages() Dim oMailitem As MailItem Dim oMailItems As Items, i as Long 'All messages in current folder Set oMailItems = Application.ActiveExplorer.CurrentFolder.Items 'Find first Unread message Set oMailitem = oMailItems.Find("[To] = ""*All CFPSA HQ""") 'Loop through all unread messages Do 'Count i=i+1 'Find next message Set oMailitem = oMailItems.FindNext Loop Until oMailitem Is Nothing 'Number of messages msgbox i Set oMailitem = Nothing Set oMailItems = Nothing End Sub

Edit: added trailing VB tag
__________________
-Carl

Last edited by Timbo; 02-25-2003 at 01:50 AM.
Reply With Quote
  #4  
Old 02-25-2003, 07:53 AM
florida florida is offline
Centurion
 
Join Date: Jan 2003
Posts: 190
Default

thank you for your help. As a beginner in VB do I need to create this in my vb 6.0 studio starting with Standard exe then create project etc...?
And then I just make a command button to execute it?

Please advise.
Reply With Quote
  #5  
Old 02-25-2003, 08:52 AM
Creeps Creeps is offline
Newcomer
 
Join Date: Feb 2003
Location: Nor-Cal
Posts: 21
Default

The easiest way to get this code working is just to open up Outlook, and then go Tools - Macro - VB Editor. You are going to need to insert a module and copy and paste this code in there then run it.

Use Wamphyri's code if you are going to be in Outlook exclusively, and use mine code if you are going to be outside of Outlook, and using it in a stand alone program, although, mine code will also work in Outlook.

I hope this helps.
__________________
If you tell me how you get your feeling of importance, I'll tell you what you are.
Reply With Quote
  #6  
Old 02-25-2003, 10:26 AM
florida florida is offline
Centurion
 
Join Date: Jan 2003
Posts: 190
Default

Thanks it works great. I am trying to now add two more addresses. Is this correct?

Set oMailitem = oMailItems.Find("[To] = ""*All CFPSA HQ, **Second Address, **Third Address""")
Reply With Quote
  #7  
Old 02-25-2003, 10:53 AM
Creeps Creeps is offline
Newcomer
 
Join Date: Feb 2003
Location: Nor-Cal
Posts: 21
Default

I do not think that would work, and unfortunately, there is a virus warning, so the IT team has disable the use of all macros in Outlook for the time being, so I can not test it. However, you could replace "*All CFPSHA HQ" with a string, and change the value of that string programmatically. Perhaps Wamphyri could back me up here, and give you a solid answer.
__________________
If you tell me how you get your feeling of importance, I'll tell you what you are.
Reply With Quote
  #8  
Old 02-25-2003, 11:03 AM
Wamphyri's Avatar
Wamphyri Wamphyri is offline
Variable not defined

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Ottawa, Ontario
Posts: 4,793
Default

You need to use the OR condition.
Code:
Set oMailitem = oMailItems.Find("[To] = ""*All CPSA HQ"" OR [To] = ""*All CPR HQ""")
__________________
-Carl
Reply With Quote
  #9  
Old 02-25-2003, 11:54 AM
florida florida is offline
Centurion
 
Join Date: Jan 2003
Posts: 190
Default

Thanks!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->