Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub TestOutlook()
- Dim olApp As Outlook.Application, olNs As Outlook.Namespace
- Dim olFolder As Outlook.MAPIFolder, Item As Outlook.MailItem
- Dim eFolder As Outlook.Folder '~~> additional declaration
- Dim i As Long
- Dim x As Date, ws As Worksheet '~~> declare WS variable instead
- Dim lrow As Long '~~> additional declaration
- Dim MessageInfo
- Dim Result
- Set ws = ActiveSheet '~~> or you can be more explicit using the next line
- 'Set ws = Thisworkbook.Sheets("YourTargetSheet")
- Set olApp = New Outlook.Application
- Set olNs = olApp.GetNamespace("MAPI")
- x = Date
- For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
- 'Debug.Print eFolder.Name
- Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(eFolder.Name)
- For i = olFolder.Items.Count To 1 Step -1
- If TypeOf olFolder.Items(i) Is MailItem Then
- Set Item = olFolder.Items(i)
- 'MsgBox Item.Body
- 'filter (Item)
- 'If InStr(Item.Subject, "Test download") > 0 Then
- ' MsgBox "Here"
- ' MessageInfo = "" & _
- ' "Sender : " & Item.SenderEmailAddress & vbCrLf & _
- ' "Sent : " & Item.SentOn & vbCrLf & _
- ' "Received : " & Item.ReceivedTime & vbCrLf & _
- ' "Subject : " & Item.Subject & vbCrLf & _
- ' "Size : " & Item.Size & vbCrLf & _
- ' "Message Body : " & vbCrLf & Item.Body
- ' Result = MsgBox(MessageInfo, vbOKOnly, "New Message Received")
- ' End If
- End If
- Next i
- Set olFolder = Nothing
- Next eFolder
- End Sub
- Sub filter(Item As Outlook.MailItem)
- Dim ns As Outlook.Namespace
- Dim MailDest As Outlook.Folder
- Set ns = Application.GetNamespace("MAPI")
- Set Reg1 = CreateObject("VBScript.RegExp")
- Reg1.Global = True
- Reg1.Pattern = "(.*Test download.*)"
- If Reg1.test(Item.Subject) Then
- 'Set MailDest = ns.Folders("Personal Folders").Folders("one").Folders("a")
- 'Item.Move MailDest
- MsgBox Item.Body
- End If
- End Sub
Add Comment
Please, Sign In to add comment