Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / August 2007

Tip: Looking for answers? Try searching our database.

Keeping Count of emails received each day

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Murphybp2 - 23 Aug 2007 16:45 GMT
I am trying to implement a solution created by Sue Mosher on how to
keep a count of the number of emails that I get on a daily basis.  I
have gotten this to work on my Outlook at home, but can't seem to get
it to work at the office.  I'm using Outlook 2003.  Here is the code
that I've Used.  I can manually run the UpdateCounter VBA, and it
creates the record, but I can't get it to work when mail is actually
received.  Anyone have any suggestions on what I need to do?

VBA in "This Outlook Session"

Option Explicit

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
   Dim objNS As NameSpace

   Set objNS = Application.GetNamespace("MAPI")
   Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items

   Set objNS = Nothing
End Sub

Private Sub Application_Quit()
   'disassociate global objects
   Set olInboxItems = Nothing
End Sub

Private Sub olInboxItems_Itemsadd(ByVal Item As Object)
   If Item.Class = olMail Then
       Call UpdateCounter
   End If
End Sub

Sub UpdateCounter()
   Dim objApp As Application
   Dim objNS As NameSpace
   Dim objInbox As MAPIFolder
   Dim objMessageCountFolder As MAPIFolder
   Dim objTodayCount As PostItem
   Dim strNow As String
   Dim strFind As String

   Set objApp = CreateObject("Outlook.Application")
   Set objNS = objApp.GetNamespace("MAPI")
   Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
   Set objMessageCountFolder = objInbox.Folders("Message Count")

   If Not objMessageCountFolder Is Nothing Then
       ' get the item that matches today
       strNow = FormatDateTime(Now, vbLongDate)
       strFind = "[Subject] = """ & strNow & """"
       Set objTodayCount = objMessageCountFolder.Items.Find(strFind)
       If objTodayCount Is Nothing Then
           ' create a new item
           Set objTodayCount = _
               objMessageCountFolder.Items.Add("IPM.Post")
           objTodayCount.Subject = strNow
           objTodayCount.Mileage = 1
       Else
           objTodayCount.Mileage = CInt(objTodayCount.Mileage) + 1
       End If
       objTodayCount.Save

   End If

   Set objTodayCount = Nothing
   Set objMessageCountFolder = Nothing
   Set objInbox = Nothing
   Set objNS = Nothing
   Set objApp = Nothing
End Sub

VBA in Module

Sub UpdateCounter()
   Dim objApp As Application
   Dim objNS As NameSpace
   Dim objInbox As MAPIFolder
   Dim objMessageCountFolder As MAPIFolder
   Dim objTodayCount As PostItem
   Dim strNow As String
   Dim strFind As String

   Set objApp = CreateObject("Outlook.Application")
   Set objNS = objApp.GetNamespace("MAPI")
   Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
   Set objMessageCountFolder = objInbox.Folders("Message Count")

   If Not objMessageCountFolder Is Nothing Then
       ' get the item that matches today
       strNow = FormatDateTime(Now, vbLongDate)
       strFind = "[Subject] = """ & strNow & """"
       Set objTodayCount = objMessageCountFolder.Items.Find(strFind)
       If objTodayCount Is Nothing Then
           ' create a new item
           Set objTodayCount = _
               objMessageCountFolder.Items.Add("IPM.Post")
           objTodayCount.Subject = strNow
           objTodayCount.Mileage = 1
       Else
           objTodayCount.Mileage = CInt(objTodayCount.Mileage) + 1
       End If
       objTodayCount.Save

   End If

   Set objTodayCount = Nothing
   Set objMessageCountFolder = Nothing
   Set objInbox = Nothing
   Set objNS = Nothing
   Set objApp = Nothing
End Sub
Michael Bauer [MVP - Outlook] - 24 Aug 2007 06:20 GMT
If it runs manually then it should do so automatically, too. Please note,
after code changes you need to restart Outlook or run Application_Startup
manually.

Signature

Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
 Organize eMails:
 <http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Thu, 23 Aug 2007 15:45:43 -0000 schrieb Murphybp2:

> I am trying to implement a solution created by Sue Mosher on how to
> keep a count of the number of emails that I get on a daily basis.  I
[quoted text clipped - 109 lines]
>     Set objApp = Nothing
> End Sub
Murphybp2 - 29 Aug 2007 17:55 GMT
On Aug 24, 1:20 am, "Michael Bauer [MVP - Outlook]" <m...@mvps.org>
wrote:
> If it runs manually then it should do so automatically, too. Please note,
> after code changes you need to restart Outlook or run Application_Startup
[quoted text clipped - 123 lines]
>
> - Show quoted text -

I have restarted Outlook, and PC with no avail.  Not sure what you
mean by running Application_Startup manually.  How do I do that?
Michael Bauer [MVP - Outlook] - 30 Aug 2007 06:17 GMT
Place the cursor into the procedure and press F5.

Creating another instance of Outlook within Outlook is evil. So delete these
two lines:

   Dim objApp As Application
   Set objApp = CreateObject("Outlook.Application")

and replace all remaining objApp in your code by Application.

Signature

Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
 Organize eMails:
 <http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Wed, 29 Aug 2007 16:55:35 -0000 schrieb Murphybp2:

> On Aug 24, 1:20 am, "Michael Bauer [MVP - Outlook]" <m...@mvps.org>
> wrote:
[quoted text clipped - 6 lines]
>> Michael Bauer - MVP Outlook
>>   Organize eMails:

<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

>> Am Thu, 23 Aug 2007 15:45:43 -0000 schrieb Murphybp2:
>>
[quoted text clipped - 116 lines]
> I have restarted Outlook, and PC with no avail.  Not sure what you
> mean by running Application_Startup manually.  How do I do that?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.