0
votes

I am using the code below to create output showing how many emails were in a defined folder per day. This all works fine... My question is in the section with XXXXX, how do I reference each mail item so that I can do a regex for a word pattern? The end goal is to find out how many emails contained a keyword on a given day. The desired output is something like this:

,, 2015-01-01,15,2,5 2015-01-01,23,22,0 ... ...

I'm ok to figure out the code on determining the number of emails based on the keyword, just not certain how to reference the email messages based on the code as is today...

Thanks for your advice.

Sub HowManyEmails()

Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")

On Error Resume Next
Set objFolder = objnSpace.Folders("Personal Folders").Folders("Inbox").Folders("jobs.keep")
If Err.Number <> 0 Then
    Err.Clear
    MsgBox "No such folder."
    Exit Sub
End If

EmailCount = objFolder.Items.Count
MsgBox "Number of emails in the folder: " & EmailCount, , "email count"

Dim dateStr As String
Dim myItems As Outlook.Items
Dim dict As Object
Dim msg As String
Set dict = CreateObject("Scripting.Dictionary")
Set myItems = objFolder.Items
myItems.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItems

    xxxxxxx
    xxxxxxx
    xxxxxxx

    dateStr = GetDate(myItem.SentOn)
    If Not dict.Exists(dateStr) Then
        dict(dateStr) = 0
    End If
    dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem

'Write output to file Dim enviro As String enviro = CStr(Environ("USERPROFILE")) FILEPATH = enviro & "\Desktop\emails.csv" Open FILEPATH For Output As 1 msg = "" For Each o In dict.Keys msg = msg & o & "," & dict(o) & vbCrLf 'MsgBox msg Next Print #1, msg Close #1 'Write output to file

Set objFolder = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing

End Sub

Function GetDate(dt As Date) As String GetDate = Year(dt) & "-" & Month(dt) & "-" & Day(dt) End Function

2

2 Answers

1
votes

You need to check the type of item in your code:

Dim myMailItem As Outlook.mailItem
....

For each myItem in myItems
    If TypeOf myItem Is MailItem Then
        Set myMailItem = myItem

         XXXXXXXXXXX and rest of code here use myMailItem instead of myItem to get info

    End If
Next myItem
1
votes

First of all, I'd recommend using the Find/FindNext or Restrict methods of the Items class to find the subset of items that match to the specified condition. Iterating through all items in the folder may take a lot of time.

objnSpace.Folders("Personal Folders").Folders("Inbox")

Use the GetDefaultFolder method of the Namespace class to get a folder that represents the default folder of the requested type for the current profile.

Outlook uses EntryID values for identifying Outlook items uniquely. See Working with EntryIDs and StoreIDs for more information. If you know the IDs of an item and the folder it's stored in, you can directly reference the item using the NameSpace.GetItemFromID method.