none
outlook vba to save sent email to a public folder RRS feed

  • Question

  • Hello there,

    I'm use to write VBA code for Excel, but I'm pretty green when doing so in Outlook.

    I'd like to be able to select a folder to place an email after it's sent.  The best solution would be to avoid saving a copy of it in the sent folder and place only one copy in the folder that would be selected each time.

    I found the code below to do so.  It's working well except that I can't use a "public"  or "shared" folder.  (not sure of the right appelation for this).  If I select one of my own folder it's working well. 

    Could anyone share there lights on this?

    Private Sub Application_ItemSend(ByVal Item As Object, _
        Cancel As Boolean)
      Dim objNS As NameSpace
      Dim objFolder As MAPIFolder
      Set objNS = Application.GetNamespace("MAPI")
      Set objFolder = objNS.PickFolder
      If TypeName(objFolder) <> "Nothing" And _
         IsInDefaultStore(objFolder) Then
          Set Item.SaveSentMessageFolder = objFolder
      End If
      Set objFolder = Nothing
      Set objNS = Nothing
    End Sub

    Public Function IsInDefaultStore(objOL As Object) As Boolean
      Dim objApp As Outlook.Application
      Dim objNS As Outlook.NameSpace
      Dim objInbox As Outlook.MAPIFolder
      On Error Resume Next
      Set objApp = CreateObject("Outlook.Application")
      Set objNS = objApp.GetNamespace("MAPI")
      Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
      Select Case objOL.Class
        Case olFolder
          If objOL.StoreID = objInbox.StoreID Then
            IsInDefaultStore = True
          End If
        Case olAppointment, olContact, olDistributionList, _
             olJournal, olMail, olNote, olPost, olTask
          If objOL.Parent.StoreID = objInbox.StoreID Then
            IsInDefaultStore = True
          End If
        Case Else
          MsgBox "This function isn't designed to work " & _
                 "with " & TypeName(objOL) & _
                 " items and will return False.", _
                 , "IsInDefaultStore"
      End Select
      Set objApp = Nothing
      Set objNS = Nothing
      Set objInbox = Nothing
    End Function

    Tuesday, August 26, 2014 9:17 PM

Answers

  • The SaveSentMessageFolder property can be set to a mailbox folder only. If you need to move the item to a shared folder you can handle the Items.ItemAdd event of the Sent Items folder and move the item programmatically.
    Tuesday, August 26, 2014 9:52 PM
  • It looks like you are interested in the PickFolder method of the Namespace class. I'd recommend checking whether the just added item belongs to the ones which should be moved to another folder/store and don't display a dialog window for all items.
    Wednesday, August 27, 2014 1:12 PM

All replies

  • The SaveSentMessageFolder property can be set to a mailbox folder only. If you need to move the item to a shared folder you can handle the Items.ItemAdd event of the Sent Items folder and move the item programmatically.
    Tuesday, August 26, 2014 9:52 PM
  • Thank you very much, that clears one of my problems.

    So, my sub-question would be, can I use the Items.ItemAdd event and still get the user to chose which shared folder the message needs to be placed in, using, let's say an "folder Explorer"  dialogue window?

    If so would you be of an infinite kindness and share some lights on the path I could use to do so?

    Thanking you in advance,

    Wednesday, August 27, 2014 12:17 PM
  • It looks like you are interested in the PickFolder method of the Namespace class. I'd recommend checking whether the just added item belongs to the ones which should be moved to another folder/store and don't display a dialog window for all items.
    Wednesday, August 27, 2014 1:12 PM