none
Outlook 2010 VBA to save attachments from all emails in a given folder to a network share RRS feed

  • Question

  • Hi.

     

    Im trying to adapt some code here to save all the attachments off all emails in a folder to a network share.

     

    This is what Iv got so far: 

     

     

    Public Sub SaveOLFolderAttachments()
     
      ' Ask the user to select an Outlook folder to process
      Dim olPurgeFolder As Outlook.MAPIFolder
      Set olPurgeFolder = Outlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("LOGINS")
      If olPurgeFolder Is Nothing Then Exit Sub
    
      ' Iteration variables
      Dim msg As Outlook.MailItem
      Dim att As Outlook.Attachment
      Dim sSavePathFS As String
      Dim sDelAtts
      For Each msg In olPurgeFolder.Items
        sDelAtts = ""
    
        ' We check each msg for attachments as opposed to using .Restrict("[Attachment] > 0")
        ' on our olPurgeFolder.Items collection.  The collection returned by the Restrict method
        ' will be dynamically updated each time we remove an attachment.  Each update will
        ' reindex the collection.  As a result, it does not provide a reliable means for iteration.
        ' This is why the For Each loops will not work.
        If msg.Attachments.Count > 0 Then
          ' This While loop is controlled via the .Delete method
          ' which will decrement msg.Attachments.Count by one each time.
          While msg.Attachments.Count > 0
            ' Save the file
            sSavePathFS = "\\UNC\PATH\TO\SAVE\Attachments\" & msg.Attachments(1).FileName
            msg.Attachments(1).SaveAsFile sSavePathFS
            msg.Attachments(1).Delete
           
          Wend
           
            msg.Save
        End If
      Next
    End Sub
    

     


    But Im getting the following Run-time error: '438': Object doesn't support this property or method. on the Set olPurgeFolder line.

    Iv also tried this instead, but this gives me a Compile error: Method or datamember not found on the .GetDefaultFolder part.

     

      ' Ask the user to select an Outlook folder to process
      Dim olPurgeFolder As Outlook.MAPIFolder
      Set olPurgeFolder = Outlook.GetDefaultFolder(olFolderInbox)
    Set olPurgeFolder = Outlook.Folders("LOGINS")
      If olPurgeFolder Is Nothing Then Exit Sub
    
    

     

    I am a complete n00b when it comes to VB, but from what Iv been reading on the net and looking at similar examples, this code should work:/

    Is there another way off making the script run in a gives subfolder of inbox ?

    Thanks in advance:)

     



    • Edited by JadeSoturi Tuesday, January 31, 2012 10:36 PM
    Tuesday, January 31, 2012 10:35 PM

Answers

  • if this is macro inside outlook then change offending line to:

    ThisOutlookSession.Session.GetDetaultFolder(olFolderInbox).Folders("LOGINS")

    Wednesday, February 1, 2012 9:21 AM
  • Or Application.GetNameSpace("MAPI") or Application.Session and so on.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:1660110c-be58-4e94-8395-71636c36d6cb...

    if this is macro inside outlook then change offending line to:

    ThisOutlookSession.Session.GetDetaultFolder(olFolderInbox).Folders("LOGINS")


    Ken Slovak MVP - Outlook
    Wednesday, February 1, 2012 3:30 PM
    Moderator

All replies

  • if this is macro inside outlook then change offending line to:

    ThisOutlookSession.Session.GetDetaultFolder(olFolderInbox).Folders("LOGINS")

    Wednesday, February 1, 2012 9:21 AM
  • Or Application.GetNameSpace("MAPI") or Application.Session and so on.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:1660110c-be58-4e94-8395-71636c36d6cb...

    if this is macro inside outlook then change offending line to:

    ThisOutlookSession.Session.GetDetaultFolder(olFolderInbox).Folders("LOGINS")


    Ken Slovak MVP - Outlook
    Wednesday, February 1, 2012 3:30 PM
    Moderator