Forum Discussion
VBA code to remove all mails from a specified Folders in Excel
Hi All
I need a help from you guys.
I have the below code, that help me to download all the attachments from outlook mails from a specified folder in outlook by clicking Command button from Excel. I want either delete or move these mails from the folder to temp folder after doing the same above in a same click. Please let me know what changes to be needed or not??
Option Explicit
Private Sub CommandButton1_Click()
Dim OlApp As Object
Dim Olmail As Object
Dim Olitems As Object
Dim Olfolder As Object
Dim j As Integer
Dim Strfolder As String
Dim FilePath As String
Dim OlfolderSub As Object
Dim ns As Object
Dim inboxFol As Object
Dim subFol As Object 'Outlook.Folder
Set OlApp = GetObject(, "Outlook.application")
If Err.Number = 429 Then
Set OlApp = CreateObject("Outlook.Application")
End If
Strfolder = "C:\Work Area\OutlookAttachments" 'change extract to the folder name where you would like to have your attacment
Set Olfolder = OlApp.GetNamespace("MAPI").Folders("xyz@company.com").Folders("Inbox").Folders("Work in Progress (FC)")
Set ns = OlApp.GetNamespace("MAPI")
Set inboxFol = ns.GetDefaultFolder(6)
Set subFol = inboxFol.Folders("Temp")
Set Olitems = Olfolder.Items
For Each Olmail In Olitems
If Olmail.Attachments.Count > 0 Then
For j = 1 To Olmail.Attachments.Count
FilePath = Strfolder & "\" & Olmail.Attachments.Item(j).fileName
Olmail.Attachments.Item(j).SaveAsFile FilePath
Olmail.UnRead = False
'Olmail.Move subFol
Next j
End If
Next
Set Olfolder = Nothing
Set Olitems = Nothing
Set Olmail = Nothing
Set OlApp = Nothing
End Sub