J
jzeyer
I am currently trying to finish up an Outlook project whereby I increase a
counter for every message that is put into a sub-folder called "Completed"
which exists under the Inbox in Outlook 2002. I am missing some kind of link
between the WithEvents method and the actual subfolder. How do you properly
reference a user-made subfolder with the "WithEvents" method? My current
code is as follows:
Option Explicit
Option Compare Text
'Private WithEvents olInboxItems As Items
Private WithEvents objFolderItems As Items
'Public Function GetFolder(strFolderPath As String) As MAPIFolder
'' folder path needs to be something like
'' "Public Folders\All Public Folders\Company\Sales"
'Dim objApp As Outlook.Application
'Dim objNS As Outlook.NameSpace
'Dim colFolders As Outlook.Folders
'Dim objFolder As Outlook.MAPIFolder
'Dim arrFolders() As String
'Dim I As Long
'On Error Resume Next
'strFolderPath = Replace(strFolderPath, "/", "\")
'arrFolders() = Split(strFolderPath, "\")
'Set objApp = CreateObject("Outlook.Application")
'Set objNS = objApp.GetNamespace("MAPI")
'Set objFolder = objNS.Folders.Item(arrFolders(0))
'If Not objFolder Is Nothing Then
'For I = 1 To UBound(arrFolders)
'Set colFolders = objFolder.Folders
'Set objFolder = Nothing
'Set objFolder = colFolders.Item(arrFolders(I))
'If objFolder Is Nothing Then
'Exit For
'End If
'Next
'End If
'Set GetFolder = objFolder
'Set colFolders = Nothing
'Set objNS = Nothing
'Set objApp = Nothing
'End Function
Private Sub Application_Quit()
' disassociate global objects
Set objFolderItems = Nothing
End Sub
Private Sub objFolderItems_ItemAdd(ByVal Item As Object)
Dim strSubject As String
Dim strSearch As String
Dim varFound As Variant
If Item.Class = olMail Then
strSubject = Item.Subject
strSearch = "undeliverable"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "your mailbox is over"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "out of office autoreply"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "warning"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "delivery status notification"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "failure notice"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "summary of junk email"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
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).Folders("Complete")
Set objMessageCountFolder =
objNS.GetDefaultFolder(olFolderInbox).Folders("Message Count")
'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
Sub SendMail()
Dim MyItem As MailItem
Dim objTodayCount As PostItem
Dim strNow As String
Dim strFind As String
Dim objMessageCountFolder As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim objApp As Application
strNow = FormatDateTime(Now, vbLongDate)
strFind = "[Subject] = """ & strNow & """"
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objMessageCountFolder = objInbox.Folders("Message Count")
Set objTodayCount = objMessageCountFolder.Items.Find(strFind)
Set MyItem = Application.CreateItem(olMailItem)
MyItem.Subject = objNS.CurrentUser & "'s Daily E-Mail Count For " & strNow
MyItem.Body = "Total e-mail's completed for " & strNow & ": " &
objTodayCount.Mileage
MyItem.To = "Jason Zeyer"
MyItem.Send
Set objApp = Nothing
Set objNS = Nothing
Set objInbox = Nothing
Set objMessageCountFolder = Nothing
Set objTodayCount = Nothing
End Sub
Public Sub Application_Startup()
Dim objApp As Application
Dim objNS As NameSpace
Dim objFolderItems As MAPIFolder
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolderItems =
objNS.GetDefaultFolder(olFolderInbox).Folders("Complete")
'Set objFolderItems = GetFolder("Outlook Today/Inbox/Complete")
End Sub
Thanks for any help you can lend!
counter for every message that is put into a sub-folder called "Completed"
which exists under the Inbox in Outlook 2002. I am missing some kind of link
between the WithEvents method and the actual subfolder. How do you properly
reference a user-made subfolder with the "WithEvents" method? My current
code is as follows:
Option Explicit
Option Compare Text
'Private WithEvents olInboxItems As Items
Private WithEvents objFolderItems As Items
'Public Function GetFolder(strFolderPath As String) As MAPIFolder
'' folder path needs to be something like
'' "Public Folders\All Public Folders\Company\Sales"
'Dim objApp As Outlook.Application
'Dim objNS As Outlook.NameSpace
'Dim colFolders As Outlook.Folders
'Dim objFolder As Outlook.MAPIFolder
'Dim arrFolders() As String
'Dim I As Long
'On Error Resume Next
'strFolderPath = Replace(strFolderPath, "/", "\")
'arrFolders() = Split(strFolderPath, "\")
'Set objApp = CreateObject("Outlook.Application")
'Set objNS = objApp.GetNamespace("MAPI")
'Set objFolder = objNS.Folders.Item(arrFolders(0))
'If Not objFolder Is Nothing Then
'For I = 1 To UBound(arrFolders)
'Set colFolders = objFolder.Folders
'Set objFolder = Nothing
'Set objFolder = colFolders.Item(arrFolders(I))
'If objFolder Is Nothing Then
'Exit For
'End If
'Next
'End If
'Set GetFolder = objFolder
'Set colFolders = Nothing
'Set objNS = Nothing
'Set objApp = Nothing
'End Function
Private Sub Application_Quit()
' disassociate global objects
Set objFolderItems = Nothing
End Sub
Private Sub objFolderItems_ItemAdd(ByVal Item As Object)
Dim strSubject As String
Dim strSearch As String
Dim varFound As Variant
If Item.Class = olMail Then
strSubject = Item.Subject
strSearch = "undeliverable"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "your mailbox is over"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "out of office autoreply"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "warning"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "delivery status notification"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "failure notice"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
strSearch = "summary of junk email"
varFound = InStr(1, strSubject, strSearch)
If varFound <> 0 Or varFound = Null Then
'MsgBox "This Item Will Not Be Counted", vbOKOnly, "Trip Test
Code"
Exit Sub
End If
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).Folders("Complete")
Set objMessageCountFolder =
objNS.GetDefaultFolder(olFolderInbox).Folders("Message Count")
'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
Sub SendMail()
Dim MyItem As MailItem
Dim objTodayCount As PostItem
Dim strNow As String
Dim strFind As String
Dim objMessageCountFolder As MAPIFolder
Dim objInbox As MAPIFolder
Dim objNS As NameSpace
Dim objApp As Application
strNow = FormatDateTime(Now, vbLongDate)
strFind = "[Subject] = """ & strNow & """"
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objMessageCountFolder = objInbox.Folders("Message Count")
Set objTodayCount = objMessageCountFolder.Items.Find(strFind)
Set MyItem = Application.CreateItem(olMailItem)
MyItem.Subject = objNS.CurrentUser & "'s Daily E-Mail Count For " & strNow
MyItem.Body = "Total e-mail's completed for " & strNow & ": " &
objTodayCount.Mileage
MyItem.To = "Jason Zeyer"
MyItem.Send
Set objApp = Nothing
Set objNS = Nothing
Set objInbox = Nothing
Set objMessageCountFolder = Nothing
Set objTodayCount = Nothing
End Sub
Public Sub Application_Startup()
Dim objApp As Application
Dim objNS As NameSpace
Dim objFolderItems As MAPIFolder
Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolderItems =
objNS.GetDefaultFolder(olFolderInbox).Folders("Complete")
'Set objFolderItems = GetFolder("Outlook Today/Inbox/Complete")
End Sub
Thanks for any help you can lend!