Advertisement
Guest User

Vba

a guest
Nov 15th, 2017
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.12 KB | None | 0 0
  1. Option Explicit
  2. Public Sub Example()
  3.    '// Declare your Variables
  4.     Dim olNs As Outlook.NameSpace
  5.     Dim Inbox As Outlook.MAPIFolder
  6.     Dim Items As Outlook.Items
  7.     Dim Item As Outlook.MailItem
  8.     Dim Atmt As Attachment
  9.     Dim Filter As String
  10.     Dim FilePath As String
  11.     Dim AtmtName As String
  12.     Dim i As Long
  13.  
  14.    '// Set Inbox Reference
  15.     Set olNs = Application.GetNamespace("MAPI")
  16.     Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
  17.  
  18.     FilePath = "C:\Temp\"
  19.     Filter = "[attachment] = True And [Unread] = True"
  20.  
  21.     Set Items = Inbox.Items.Restrict(Filter)
  22.  
  23.    '// Loop through backwards
  24.     For i = Items.Count To 1 Step -1
  25.         Set Item = Items.Item(i)
  26.  
  27.         DoEvents
  28.  
  29.         If Item.Class = olMail Then
  30.             Debug.Print Item.Subject ' Immediate Window
  31.  
  32.             For Each Atmt In Item.Attachments
  33.                 AtmtName = FilePath & Atmt.FileName
  34.                 Atmt.SaveAsFile AtmtName
  35.             Next
  36.         End If
  37.     Next
  38.  
  39.     Set Inbox = Nothing
  40.     Set Items = Nothing
  41.     Set Item = Nothing
  42.     Set Atmt = Nothing
  43.     Set olNs = Nothing
  44. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement