Discussion:
How to read RECIPIENT and set SaveSentMessageFolder accordingly
(too old to reply)
unknown
2010-01-18 16:51:53 UTC
Permalink
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
Sue Mosher [MVP]
2010-01-18 17:20:44 UTC
Permalink
A glance at the object browser (F2 in Outlook VBA) would show you that there
is no MailItem.Recipient object, as you have in this statement:

myRecipient = itemMail.Recipient

Instead, there is a Recipients collection, which you can iterate with a For
Each ... Next loop to get information about each Recipient in turn:

For Each myRecipient in itemMail.Recipients
MsgBox myRecipient.Address
' etc.
Net

I see lots of other problems with your code:

1) This statement should never appear in Outlook VBA code:

Set myOlApp = CreateObject("Outlook.Application")

Instead use the intrinsic Application object that VBA exposes:

Set myOlApp = Application

2) Only one Namespace is active per Outloook session, so you don't need two
separate variables. Replace these statements

Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myPSTspace = myOlApp.GetNamespace("MAPI")

with

Set myNamespace = myOlApp.Session

3) This statement is not the correct way to work with the item being sent:

Set itemMail = myOlApp.ActiveInspector.CurrentItem

Instead, use the Item object passed by the ItemSend event handler's
procedure signature

Set itemMail = Item

Or, better, omit that statement and just replace itemMail with Item
everywhere in your code.

4) You don't need a statement like this to resend the item you just sent;
delete this statement:

itemMail.Send
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by unknown
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
Set myPSTSentTemp = myPSTSent.Folders("First Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Second Level")
Set myPSTSentTemp = myPSTSentTemp.Folders("Third Level")
Set itemMail.SaveSentMessageFolder = myPSTSentTemp
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
unknown
2010-01-18 20:54:16 UTC
Permalink
Sue, my MVP :-)
Thanks so much, also for clearing up the code. All suggested changes work. That happens, if you copy and paste from different sources without totally understanding what you are doing.

myRecipient.Address gives a weird statement for company internal recipients, myRecipient.name works better. To keep the code short it would help greatly to search for certain patterns (department abbreviations e.g. ENG for engineering).

If item.Name ?contains? "ENG" Then
Set myPSTSentTemp = myPSTSent.Folders("ENG")
Set item.SaveSentMessageFolder = myPSTSentTemp
End If

Axel



Sue Mosher [MVP] wrote:

A glance at the object browser (F2 in Outlook VBA) would show you that thereis
18-Jan-10

A glance at the object browser (F2 in Outlook VBA) would show you that there
is no MailItem.Recipient object, as you have in this statement:

myRecipient = itemMail.Recipient

Instead, there is a Recipients collection, which you can iterate with a For
Each ... Next loop to get information about each Recipient in turn:

For Each myRecipient in itemMail.Recipients
MsgBox myRecipient.Address
' etc.
Net

I see lots of other problems with your code:

1) This statement should never appear in Outlook VBA code:

Set myOlApp = CreateObject("Outlook.Application")

Instead use the intrinsic Application object that VBA exposes:

Set myOlApp = Application

2) Only one Namespace is active per Outloook session, so you do not need two
separate variables. Replace these statements

Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myPSTspace = myOlApp.GetNamespace("MAPI")

with

Set myNamespace = myOlApp.Session

3) This statement is not the correct way to work with the item being sent:

Set itemMail = myOlApp.ActiveInspector.CurrentItem

Instead, use the Item object passed by the ItemSend event handler's
procedure signature

Set itemMail = Item

Or, better, omit that statement and just replace itemMail with Item
everywhere in your code.

4) You do not need a statement like this to resend the item you just sent;
delete this statement:

itemMail.Send

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54

Previous Posts In This Thread:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Spambot Killer ASP.NET Mailto: Hyperlink Control
http://www.eggheadcafe.com/tutorials/aspnet/9817ba6f-ba00-4523-9097-f0235cdcb480/spambot-killer-aspnet-ma.aspx
Sue Mosher [MVP]
2010-01-18 22:09:06 UTC
Permalink
Use the Instr() function to search for substrings; look it up in Help. Or,
learn how to use regular expressions to do pattern matching. See
http://www.outlookcode.com/article.aspx?ID=41 for links to RexEx resources.

And there's nothing weird about the recipient address. If you're in an
Exchange organization, it's perfectly normal to have a long address in X.400
format.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by unknown
Sue, my MVP :-)
Thanks so much, also for clearing up the code. All suggested changes work.
That happens, if you copy and paste from different sources without totally
understanding what you are doing.
myRecipient.Address gives a weird statement for company internal
recipients, myRecipient.name works better. To keep the code short it would
help greatly to search for certain patterns (department abbreviations e.g.
ENG for engineering).
If item.Name ?contains? "ENG" Then
Set myPSTSentTemp = myPSTSent.Folders("ENG")
Set item.SaveSentMessageFolder = myPSTSentTemp
End If
Axel
A glance at the object browser (F2 in Outlook VBA) would show you that thereis
18-Jan-10
A glance at the object browser (F2 in Outlook VBA) would show you that there
myRecipient = itemMail.Recipient
Instead, there is a Recipients collection, which you can iterate with a For
For Each myRecipient in itemMail.Recipients
MsgBox myRecipient.Address
' etc.
Net
Set myOlApp = CreateObject("Outlook.Application")
Set myOlApp = Application
2) Only one Namespace is active per Outloook session, so you do not need two
separate variables. Replace these statements
Set myNamespace = myOlApp.GetNamespace("MAPI")
Set myPSTspace = myOlApp.GetNamespace("MAPI")
with
Set myNamespace = myOlApp.Session
Set itemMail = myOlApp.ActiveInspector.CurrentItem
Instead, use the Item object passed by the ItemSend event handler's
procedure signature
Set itemMail = Item
Or, better, omit that statement and just replace itemMail with Item
everywhere in your code.
4) You do not need a statement like this to resend the item you just sent;
itemMail.Send
--
Sue Mosher, Outlook MVP
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Submitted via EggHeadCafe - Software Developer Portal of Choice
Spambot Killer ASP.NET Mailto: Hyperlink Control
http://www.eggheadcafe.com/tutorials/aspnet/9817ba6f-ba00-4523-9097-f0235cdcb480/spambot-killer-aspnet-ma.aspx
Loading...