unknown
2010-01-18 16:51:53 UTC
OK, I'm try-and-erroring the following code to help me with keepingmy SentItems Box clean. After clicking the send button, I created a message box to ask me if I wanna delete the e-mail. YES will set the SaveSentMessageFolder as a subfolder "delete" in my SentItems box, which I can delete without looking at each mail again. NO should start further analysis of the RECIPIENT and set the SaveSentMessageFolder to an external OutlookDataFile "personal.pst". But I can't make Outlook pick up the recipient's address to compare it and set the SaveSentMessageFolder accordingly. Can somebody please help me how to define the variable(s) correctly and the "Else" section in the code below?
Thanks,
Axel
Public Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myPSTspace As Outlook.NameSpace
Dim itemMail As Outlook.MailItem
Dim mySentItems As Outlook.MAPIFolder
Dim mySentDel As Outlook.MAPIFolder
Dim myPSTSent As Outlook.MAPIFolder
Dim myPSTSentTemp As Outlook.MAPIFolder
Dim myRecipient As Outlook.Recipient
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myPSTspace = myOlApp.GetNamespace("MAPI")
Set mySentItems = myNamespace.GetDefaultFolder(olFolderSentMail)
Set mySentDel = mySentItems.Folders("delete")
Set itemMail = myOlApp.ActiveInspector.CurrentItem
'Check for deletion option
Prompt$ = "Store e-mail in Sent Items\delete?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check 4 deletion") = vbYes Then
'If deletion is picked, than store in (and possibly create) folder Sent Items\delete
'Create Folder "delete" to "Sent Items" if necessary
If mySentDel Is Nothing Then
Set mySentDel = mySentItems.Folders.Add("delete")
End If
Set itemMail.SaveSentMessageFolder = mySentDel
'If not, check recipients and store in designated archive file / folders
'!!!THIS IS WHERE MY PROBLEM IS!!!
Else
'Add storage file
myPSTspace.AddStore "C:\Outlook\personal.pst"
'Get root folder of that file
Set myPSTSent = myPSTspace.Folders.GetLast
myRecipient = itemMail.Recipient
If itemMail.Recipient = "***@gmail.com" _
Set myPSTSentTemp = myPSTSent.Folders("First Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Second Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Third Level")
Set itemMail.SaveSentMessageFolder = myPSTSentTemp
ElseIf itemMail.Recipients = "***@gmail.com" Then
Set itemMail.SaveSentMessageFolder = mySentDel
End If
End If
itemMail.Send
Set myOlApp = Nothing
'...
End Sub
Submitted via EggHeadCafe - Software Developer Portal of Choice
Resharper for Visual Studio .NET 2005 - Product Review
http://www.eggheadcafe.com/tutorials/aspnet/a1fb97b8-be1b-4ba5-9db9-94b0248bc402/resharper-for-visual-stud.aspx
Thanks,
Axel
Public Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
On Error Resume Next
Dim myOlApp As Outlook.Application
Dim myNamespace As Outlook.NameSpace
Dim myPSTspace As Outlook.NameSpace
Dim itemMail As Outlook.MailItem
Dim mySentItems As Outlook.MAPIFolder
Dim mySentDel As Outlook.MAPIFolder
Dim myPSTSent As Outlook.MAPIFolder
Dim myPSTSentTemp As Outlook.MAPIFolder
Dim myRecipient As Outlook.Recipient
Set myOlApp = CreateObject("Outlook.Application")
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myPSTspace = myOlApp.GetNamespace("MAPI")
Set mySentItems = myNamespace.GetDefaultFolder(olFolderSentMail)
Set mySentDel = mySentItems.Folders("delete")
Set itemMail = myOlApp.ActiveInspector.CurrentItem
'Check for deletion option
Prompt$ = "Store e-mail in Sent Items\delete?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check 4 deletion") = vbYes Then
'If deletion is picked, than store in (and possibly create) folder Sent Items\delete
'Create Folder "delete" to "Sent Items" if necessary
If mySentDel Is Nothing Then
Set mySentDel = mySentItems.Folders.Add("delete")
End If
Set itemMail.SaveSentMessageFolder = mySentDel
'If not, check recipients and store in designated archive file / folders
'!!!THIS IS WHERE MY PROBLEM IS!!!
Else
'Add storage file
myPSTspace.AddStore "C:\Outlook\personal.pst"
'Get root folder of that file
Set myPSTSent = myPSTspace.Folders.GetLast
myRecipient = itemMail.Recipient
If itemMail.Recipient = "***@gmail.com" _
Set myPSTSentTemp = myPSTSent.Folders("First Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Second Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Third Level")
Set itemMail.SaveSentMessageFolder = myPSTSentTemp
ElseIf itemMail.Recipients = "***@gmail.com" Then
Set itemMail.SaveSentMessageFolder = mySentDel
End If
End If
itemMail.Send
Set myOlApp = Nothing
'...
End Sub
Submitted via EggHeadCafe - Software Developer Portal of Choice
Resharper for Visual Studio .NET 2005 - Product Review
http://www.eggheadcafe.com/tutorials/aspnet/a1fb97b8-be1b-4ba5-9db9-94b0248bc402/resharper-for-visual-stud.aspx