• Subject: Re: How to read RECIPIENT and set SaveSentMessageFolder accordingly
  • Author: Sue Mosher [MVP]
  • Date: 18 Jan 2010
  • References: 1
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
<Axel Gehrke> wrote in message news:2010118115157axel.gehrke@bshg.com...
> 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 = "jon.grande@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 = "myself@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
18 Jan 2010How to read RECIPIENT and set SaveSentMessageFolder accordingly.Axel Gehrke
18 Jan 2010\ Re: How to read RECIPIENT and set SaveSentMessageFolder accordingly.Sue Mosher [MVP]
18 Jan 2010   \ Look for patterns in Recipient Address.Axel Gehrke
18 Jan 2010      \ Re: Look for patterns in Recipient Address.Sue Mosher [MVP]
Contact Us
All times are in (US) Eastern Daylight Time (GMT -4:00)