title: Sorting attachments into different folders
body: <p>I updated a (working) macro which I received from my friend (this macro sorts attachments into different folders) The update relates to select - case block in SaveAttachment Procedure. I'd like to save attachments into 2 different paths - PATH  and PATH2 depending on the folder. For TargetFolderItemsAA and TargetFolderItemsAB there will be PATH2 and for the rest - PATH. Does anybody know, what's wrong with the code (an error occurs)? Thanks for your help in advance.</p>
  
  <pre><code>Option Explicit
  
   '###############################################################################
   '### Module level Declarations
   'expose the items in the target folder to events
  
   'MASTS
  Dim WithEvents TargetFolderItemsG As Items
  Dim WithEvents TargetFolderItemsK As Items
  Dim WithEvents TargetFolderItemsL As Items
  Dim WithEvents TargetFolderItemsM As Items
  Dim WithEvents TargetFolderItemsN As Items
  Dim WithEvents TargetFolderItemsO As Items
  Dim WithEvents TargetFolderItemsP As Items
  Dim WithEvents TargetFolderItemsQ As Items
  Dim WithEvents TargetFolderItemsR As Items
  Dim WithEvents TargetFolderItemsT As Items
  Dim WithEvents TargetFolderItemsU As Items
  Dim WithEvents TargetFolderItemsV As Items
  Dim WithEvents TargetFolderItemsW As Items
  
   'ENERGY SALE
  Dim WithEvents TargetFolderItemsAA As Items
  Dim WithEvents TargetFolderItemsAB As Items
  
  
  Const FILE_PATH As String = "Z:\Wind_datas\"
  Const FILE_PATH2 As String = "Z:\Projects\"
  
  
  
  
   '###############################################################################
   '### this is the Application_Startup event code in the ThisOutlookSession module
  
  
  
  Private Sub Application_Startup()
       'some startup code to set our "event-sensitive" items collection
      Dim ns As Outlook.NameSpace
       '
      Set ns = Application.GetNamespace("MAPI")
  
       'MASTS
      Set TargetFolderItemsG = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Grajewo").Items
      Set TargetFolderItemsK = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Czyzew").Items
      Set TargetFolderItemsL = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Annopol").Items
      Set TargetFolderItemsM = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Wloszczowa").Items
      Set TargetFolderItemsN = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Grajewo_100m").Items
      Set TargetFolderItemsO = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Bialogard").Items
      Set TargetFolderItemsP = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Krasnik").Items
      Set TargetFolderItemsQ = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Suraz").Items
      Set TargetFolderItemsR = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Bejsce").Items
      Set TargetFolderItemsT = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Malogoszcz").Items
      Set TargetFolderItemsU = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Odolanow").Items
      Set TargetFolderItemsV = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Frampol").Items
      Set TargetFolderItemsW = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("maszty").Folders("datas_Zaklikow").Items
  
       'ENERGY SALE
      Set TargetFolderItemsAA = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("Sprzedaz energii Bialogard").Folders("Alpiq - grafiki dla Energa").Items
      Set TargetFolderItemsAB = ns.Folders.Item("Zimbra - TB").Folders.Item("Inbox").Folders("Sprzedaz energii Bialogard").Folders("WH prognoza n-1").Items
  
  End Sub
  
  
  
  'MASTS &amp; ENERGY SALE
  
  Sub SaveAttachment(ByVal Prefix As String, ByVal Item As Object)
       'when a new item is added to our "watched folder" we can process it
      Dim olAtt As Attachment
      Dim i As Integer
  
      Select Case Item
          Case TargetFolderItemsAA, TargetFolderItemsAB
                  If Item.Attachments.Count &gt; 0 Then
                      For i = 1 To Item.Attachments.Count
                          Set olAtt = Item.Attachments(i)
                           'save the attachment
                          olAtt.SaveAsFile FILE_PATH2 &amp; Prefix &amp; "\daily_files\" &amp; olAtt.FileName
                      Next
                  End If
          Case Else
                  If Item.Attachments.Count &gt; 0 Then
                      For i = 1 To Item.Attachments.Count
                          Set olAtt = Item.Attachments(i)
                           'save the attachment
                          olAtt.SaveAsFile FILE_PATH &amp; Prefix &amp; "\daily_files\" &amp; olAtt.FileName
                      Next
                  End If
      End Select
  
      Set olAtt = Nothing
  End Sub
  
   '###############################################################################
   '### this is the ItemAdd event code
  
   'MASTS
  Sub TargetFolderItemsG_ItemAdd(ByVal Item As Object)
      SaveAttachment "Grajewo", Item
  End Sub
  
  Sub TargetFolderItemsK_ItemAdd(ByVal Item As Object)
      SaveAttachment "Czyzew", Item
  End Sub
  
  Sub TargetFolderItemsL_ItemAdd(ByVal Item As Object)
      SaveAttachment "Annopol", Item
  End Sub
  
  Sub TargetFolderItemsM_ItemAdd(ByVal Item As Object)
      SaveAttachment "Wloszczowa", Item
  End Sub
  
  Sub TargetFolderItemsN_ItemAdd(ByVal Item As Object)
      SaveAttachment "grajewo_100m", Item
  End Sub
  
  Sub TargetFolderItemsO_ItemAdd(ByVal Item As Object)
      SaveAttachment "Bialogard", Item
  End Sub
  
  Sub TargetFolderItemsP_ItemAdd(ByVal Item As Object)
      SaveAttachment "Krasnik", Item
  End Sub
  
  Sub TargetFolderItemsQ_ItemAdd(ByVal Item As Object)
      SaveAttachment "Suraz", Item
  End Sub
  
  Sub TargetFolderItemsR_ItemAdd(ByVal Item As Object)
      SaveAttachment "Bejsce", Item
  End Sub
  
  Sub TargetFolderItemsT_ItemAdd(ByVal Item As Object)
      SaveAttachment "Malogoszcz", Item
  End Sub
  
  Sub TargetFolderItemsU_ItemAdd(ByVal Item As Object)
      SaveAttachment "Odolanow", Item
  End Sub
  
  Sub TargetFolderItemsV_ItemAdd(ByVal Item As Object)
      SaveAttachment "Frampol", Item
  End Sub
  
  Sub TargetFolderItemsW_ItemAdd(ByVal Item As Object)
      SaveAttachment "Zaklikow", Item
  End Sub
  
  'ENERGY SALE
  Sub TargetFolderItemsAA_ItemAdd(ByVal Item As Object)
      SaveAttachment "01_PKD_do_Energa", Item
  End Sub
  
  Sub TargetFolderItemsAB_ItemAdd(ByVal Item As Object)
      SaveAttachment "03_Prognozy_WH_n-1", Item
  End Sub
  
   '###############################################################################
   '### this is the Application_Quit event code in the ThisOutlookSession module
  Private Sub Application_Quit()
  
      Dim ns As Outlook.NameSpace
      Set TargetFolderItemsG = Nothing
      Set TargetFolderItemsK = Nothing
      Set TargetFolderItemsL = Nothing
      Set TargetFolderItemsM = Nothing
      Set TargetFolderItemsN = Nothing
      Set TargetFolderItemsO = Nothing
      Set TargetFolderItemsP = Nothing
      Set TargetFolderItemsQ = Nothing
      Set TargetFolderItemsR = Nothing
      Set TargetFolderItemsT = Nothing
      Set TargetFolderItemsU = Nothing
      Set TargetFolderItemsV = Nothing
      Set TargetFolderItemsW = Nothing
      Set TargetFolderItemsAA = Nothing
      Set TargetFolderItemsAB = Nothing
  
      Set ns = Nothing
  
  End Sub
  </code></pre>