SOLVED

VBA code to remove all mails from a specified Folders in Excel

%3CLINGO-SUB%20id%3D%22lingo-sub-2358875%22%20slang%3D%22en-US%22%3EVBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2358875%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20a%20help%20from%20you%20guys.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20below%20code%2C%20that%20help%20me%20to%20download%20all%20the%20attachments%20from%20outlook%20mails%20from%20a%20specified%20folder%20in%20outlook%20by%20clicking%20Command%20button%20from%20Excel.%26nbsp%3B%20I%20want%20either%20delete%20or%20move%20these%20mails%20from%20the%20folder%20to%20temp%20folder%20after%20doing%20the%20same%20above%20in%20a%20same%20click.%20Please%20let%20me%20know%20what%20changes%20to%20be%20needed%20or%20not%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EOption%20Explicit%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EPrivate%20Sub%20CommandButton1_Click()%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20OlApp%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20Olmail%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20Olitems%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20Olfolder%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20j%20As%20Integer%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20Strfolder%20As%20String%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20FilePath%20As%20String%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20OlfolderSub%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20ns%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20inboxFol%20As%20Object%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EDim%20subFol%20As%20Object%20'Outlook.Folder%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet%20OlApp%20%3D%20GetObject(%2C%20%22Outlook.application%22)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EIf%20Err.Number%20%3D%20429%20Then%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20OlApp%20%3D%20CreateObject(%22Outlook.Application%22)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EStrfolder%20%3D%20%22C%3A%5CWork%20Area%5COutlookAttachments%22%20'change%20extract%20to%20the%20folder%20name%20where%20you%20would%20like%20to%20have%20your%20attacment%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet%20Olfolder%20%3D%20OlApp.GetNamespace(%22MAPI%22).Folders(%22xyz%40company.com%22).Folders(%22Inbox%22).Folders(%22Work%20in%20Progress%20(FC)%22)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet%20ns%20%3D%20OlApp.GetNamespace(%22MAPI%22)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20inboxFol%20%3D%20ns.GetDefaultFolder(6)%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20subFol%20%3D%20inboxFol.Folders(%22Temp%22)%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet%20Olitems%20%3D%20Olfolder.Items%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EFor%20Each%20Olmail%20In%20Olitems%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EIf%20Olmail.Attachments.Count%20%26gt%3B%200%20Then%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EFor%20j%20%3D%201%20To%20Olmail.Attachments.Count%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EFilePath%20%3D%20Strfolder%20%26amp%3B%20%22%5C%22%20%26amp%3B%20Olmail.Attachments.Item(j).fileName%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EOlmail.Attachments.Item(j).SaveAsFile%20FilePath%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3EOlmail.UnRead%20%3D%20False%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E'Olmail.Move%20subFol%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ENext%20j%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EEnd%20If%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ENext%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet%20Olfolder%20%3D%20Nothing%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20Olitems%20%3D%20Nothing%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20Olmail%20%3D%20Nothing%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ESet%20OlApp%20%3D%20Nothing%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EEnd%20Sub%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2358875%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2358915%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2358915%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F420510%22%20target%3D%22_blank%22%3E%40Sameer_Kuppanath_Sultan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHave%20you%20tried%20uncommenting%20the%20line%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E'Olmail.Move%20subFol%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2359805%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2359805%22%20slang%3D%22en-US%22%3E%3CP%3EYeah%2C%20I%20have%20tried.%3C%2FP%3E%3CP%3EBut%20some%20of%20them%20are%20moving%2C%20and%20the%20rest%20remain%20in%20the%20same%20folder.%20(Work%20in%20Progress%20(FC)).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EFor%20Each%20Olmail%20In%20Olitems%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EIf%20Olmail.Attachments.Count%20%26gt%3B%200%20Then%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EFor%20j%20%3D%201%20To%20Olmail.Attachments.Count%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EFilePath%20%3D%20Strfolder%20%26amp%3B%20%22%5C%22%20%26amp%3B%20Olmail.Attachments.Item(j).fileName%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3EOlmail.Attachments.Item(j).SaveAsFile%20FilePath%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3EOlmail.UnRead%20%3D%20False%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EOlmail.Move%20subFol%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ENext%20j%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360023%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360023%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F420510%22%20target%3D%22_blank%22%3E%40Sameer_Kuppanath_Sultan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20move%20only%20messages%20that%20have%20attachments%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPrivate%20Sub%20CommandButton1_Click()%0A%20%20%20%20Dim%20olApp%20As%20Object%0A%20%20%20%20Dim%20olMail%20As%20Object%0A%20%20%20%20Dim%20olItems%20As%20Object%0A%20%20%20%20Dim%20ns%20As%20Object%0A%20%20%20%20Dim%20olFolder%20As%20Object%0A%20%20%20%20Dim%20olFolderSub%20As%20Object%0A%20%20%20%20Dim%20olInbox%20As%20Object%0A%20%20%20%20Dim%20j%20As%20Integer%0A%20%20%20%20Dim%20Strfolder%20As%20String%0A%20%20%20%20Dim%20FilePath%20As%20String%0A%0A%20%20%20%20Set%20olApp%20%3D%20GetObject(%2C%20%22Outlook.Application%22)%0A%20%20%20%20If%20Err.Number%20%3D%20429%20Then%0A%20%20%20%20%20%20%20%20Set%20olApp%20%3D%20CreateObject(%22Outlook.Application%22)%0A%20%20%20%20End%20If%0A%0A%20%20%20%20Strfolder%20%3D%20%22C%3A%5CWork%20Area%5COutlookAttachments%22%0A%0A%20%20%20%20Set%20ns%20%3D%20olApp.GetNamespace(%22MAPI%22)%0A%20%20%20%20Set%20olFolder%20%3D%20ns.Folders(%22xyz%40company.com%22).Folders(%22Inbox%22).Folders(%22Work%20in%20Progress%20(FC)%22)%0A%20%20%20%20Set%20olInbox%20%3D%20ns.GetDefaultFolder(6)%0A%20%20%20%20Set%20olFolderSub%20%3D%20olInbox.Folders(%22Temp%22)%0A%20%20%20%20Set%20olItems%20%3D%20olFolder.Items%0A%0A%20%20%20%20For%20Each%20olMail%20In%20olItems%0A%20%20%20%20%20%20%20%20If%20olMail.Attachments.Count%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20For%20j%20%3D%201%20To%20olMail.Attachments.Count%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FilePath%20%3D%20Strfolder%20%26amp%3B%20%22%5C%22%20%26amp%3B%20olMail.Attachments.Item(j).Filename%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20olMail.Attachments.Item(j).SaveAsFile%20FilePath%0A%20%20%20%20%20%20%20%20%20%20%20%20Next%20j%0A%20%20%20%20%20%20%20%20%20%20%20%20olMail.UnRead%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20olMail.Move%20olFolderSub%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20olMail%0A%0A%20%20%20%20Set%20olFolder%20%3D%20Nothing%0A%20%20%20%20Set%20olItems%20%3D%20Nothing%0A%20%20%20%20Set%20olMail%20%3D%20Nothing%0A%20%20%20%20Set%20olApp%20%3D%20Nothing%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EIf%20you%20want%20to%20move%20ALL%20messages%20in%20the%20folder%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EPrivate%20Sub%20CommandButton1_Click()%0A%20%20%20%20Dim%20olApp%20As%20Object%0A%20%20%20%20Dim%20olMail%20As%20Object%0A%20%20%20%20Dim%20olItems%20As%20Object%0A%20%20%20%20Dim%20ns%20As%20Object%0A%20%20%20%20Dim%20olFolder%20As%20Object%0A%20%20%20%20Dim%20olFolderSub%20As%20Object%0A%20%20%20%20Dim%20olInbox%20As%20Object%0A%20%20%20%20Dim%20j%20As%20Integer%0A%20%20%20%20Dim%20Strfolder%20As%20String%0A%20%20%20%20Dim%20FilePath%20As%20String%0A%0A%20%20%20%20Set%20olApp%20%3D%20GetObject(%2C%20%22Outlook.Application%22)%0A%20%20%20%20If%20Err.Number%20%3D%20429%20Then%0A%20%20%20%20%20%20%20%20Set%20olApp%20%3D%20CreateObject(%22Outlook.Application%22)%0A%20%20%20%20End%20If%0A%0A%20%20%20%20Strfolder%20%3D%20%22C%3A%5CWork%20Area%5COutlookAttachments%22%0A%0A%20%20%20%20Set%20ns%20%3D%20olApp.GetNamespace(%22MAPI%22)%0A%20%20%20%20Set%20olFolder%20%3D%20ns.Folders(%22xyz%40company.com%22).Folders(%22Inbox%22).Folders(%22Work%20in%20Progress%20(FC)%22)%0A%20%20%20%20Set%20olInbox%20%3D%20ns.GetDefaultFolder(6)%0A%20%20%20%20Set%20olFolderSub%20%3D%20olInbox.Folders(%22Temp%22)%0A%20%20%20%20Set%20olItems%20%3D%20olFolder.Items%0A%0A%20%20%20%20For%20Each%20olMail%20In%20olItems%0A%20%20%20%20%20%20%20%20If%20olMail.Attachments.Count%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20For%20j%20%3D%201%20To%20olMail.Attachments.Count%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FilePath%20%3D%20Strfolder%20%26amp%3B%20%22%5C%22%20%26amp%3B%20olMail.Attachments.Item(j).Filename%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20olMail.Attachments.Item(j).SaveAsFile%20FilePath%0A%20%20%20%20%20%20%20%20%20%20%20%20Next%20j%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20olMail.UnRead%20%3D%20False%0A%20%20%20%20%20%20%20%20olMail.Move%20olFolderSub%0A%20%20%20%20Next%20olMail%0A%0A%20%20%20%20Set%20olFolder%20%3D%20Nothing%0A%20%20%20%20Set%20olItems%20%3D%20Nothing%0A%20%20%20%20Set%20olMail%20%3D%20Nothing%0A%20%20%20%20Set%20olApp%20%3D%20Nothing%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360385%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360385%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20code%2C%20but%20when%20I%20run%20the%20code%2C%20some%20of%20the%20mails%20are%20still%20in%20that%20folder.%3C%2FP%3E%3CP%3E%26nbsp%3BThere%20were%207%20mails%20in%20the%20folder%2C%20After%20I%20run%20the%20code%2C%203%20remains%20in%20the%20folder%20as%20below%20image.%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20any%20problem%20with%20loop%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Sameer_Kuppanath_Sultan_0-1621246681803.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F281036i10EE9176C404D318%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Sameer_Kuppanath_Sultan_0-1621246681803.png%22%20alt%3D%22Sameer_Kuppanath_Sultan_0-1621246681803.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360489%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360489%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F420510%22%20target%3D%22_blank%22%3E%40Sameer_Kuppanath_Sultan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDoes%20this%20work%3F%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3EPrivate%20Sub%20CommandButton1_Click()%0A%20%20%20%20Dim%20olApp%20As%20Object%0A%20%20%20%20Dim%20olMail%20As%20Object%0A%20%20%20%20Dim%20olItems%20As%20Object%0A%20%20%20%20Dim%20ns%20As%20Object%0A%20%20%20%20Dim%20olFolder%20As%20Object%0A%20%20%20%20Dim%20olFolderSub%20As%20Object%0A%20%20%20%20Dim%20olInbox%20As%20Object%0A%20%20%20%20Dim%20i%20As%20Long%0A%20%20%20%20Dim%20j%20As%20Integer%0A%20%20%20%20Dim%20Strfolder%20As%20String%0A%20%20%20%20Dim%20FilePath%20As%20String%0A%0A%20%20%20%20Set%20olApp%20%3D%20GetObject(%2C%20%22Outlook.Application%22)%0A%20%20%20%20If%20Err.Number%20%3D%20429%20Then%0A%20%20%20%20%20%20%20%20Set%20olApp%20%3D%20CreateObject(%22Outlook.Application%22)%0A%20%20%20%20End%20If%0A%0A%20%20%20%20Strfolder%20%3D%20%22C%3A%5CWork%20Area%5COutlookAttachments%22%0A%0A%20%20%20%20Set%20ns%20%3D%20olApp.GetNamespace(%22MAPI%22)%0A%20%20%20%20Set%20olFolder%20%3D%20ns.Folders(%22xyz%40company.com%22).Folders(%22Inbox%22).Folders(%22Work%20in%20Progress%20(FC)%22)%0A%20%20%20%20Set%20olInbox%20%3D%20ns.GetDefaultFolder(6)%0A%20%20%20%20Set%20olFolderSub%20%3D%20olInbox.Folders(%22Temp%22)%0A%20%20%20%20Set%20olItems%20%3D%20olFolder.Items%0A%0A%20%20%20%20For%20i%20%3D%20olItems.Count%20To%201%20Step%20-1%0A%20%20%20%20%20%20%20%20Set%20olMail%20%3D%20olItems.Item(i)%0A%20%20%20%20%20%20%20%20If%20olMail.Attachments.Count%20%26gt%3B%200%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20For%20j%20%3D%201%20To%20olMail.Attachments.Count%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FilePath%20%3D%20Strfolder%20%26amp%3B%20%22%5C%22%20%26amp%3B%20olMail.Attachments.Item(j).Filename%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20olMail.Attachments.Item(j).SaveAsFile%20FilePath%0A%20%20%20%20%20%20%20%20%20%20%20%20Next%20j%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20olMail.UnRead%20%3D%20False%0A%20%20%20%20%20%20%20%20olMail.Move%20olFolderSub%0A%20%20%20%20Next%20i%0A%0A%20%20%20%20Set%20olFolder%20%3D%20Nothing%0A%20%20%20%20Set%20olItems%20%3D%20Nothing%0A%20%20%20%20Set%20olMail%20%3D%20Nothing%0A%20%20%20%20Set%20olApp%20%3D%20Nothing%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360491%22%20slang%3D%22de-DE%22%3ESubject%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360491%22%20slang%3D%22de-DE%22%3EIt%20doesn't%20delete%20the%20unread%20emails%20as%20far%20as%20I%20could%20understand.%20It%20has%20to%20be%20read%20in%20order%20to%20be%20considered%20as%20filed%20in%20the%20folder.%3CBR%20%2F%3EMark%20all%20your%20e-mails%20as%20read%20and%20then%20run%20through%20the%20code%2C%20maybe%20it'll%20work%20%3A))%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20It's%20just%20an%20idea%2C%20but%20who%20am%20I%3F%20I%20just%20know%20that%20I%20don't%20know%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cheers%20%3CBR%20%2F%3E%20Nikolino%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2360907%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20code%20to%20remove%20all%20mails%20from%20a%20specified%20Folders%20in%20Excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2360907%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFinally%20You%20saved%20my%20day.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EJust%20to%20add%20it%2C%20if%20I%20want%20to%20delete%20the%20mails%20permanently%20instead%20of%20moving%20to%20%22temp%22%20folder%2C%26nbsp%3B%20could%20you%20please%20tell%20me%20what%20changes%20to%20be%20done%20in%20the%20code%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EolMail.Move%20olFolderSub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%26nbsp%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Frequent Contributor

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

 

 

 

9 Replies

@Sameer_Kuppanath_Sultan 

Have you tried uncommenting the line

 

'Olmail.Move subFol

Yeah, I have tried.

But some of them are moving, and the rest remain in the same folder. (Work in Progress (FC)).

 

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

 

@Hans Vogelaar 

@Sameer_Kuppanath_Sultan 

If you want to move only messages that have attachments:

Private Sub CommandButton1_Click()
    Dim olApp As Object
    Dim olMail As Object
    Dim olItems As Object
    Dim ns As Object
    Dim olFolder As Object
    Dim olFolderSub As Object
    Dim olInbox As Object
    Dim j As Integer
    Dim Strfolder As String
    Dim FilePath As String

    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If

    Strfolder = "C:\Work Area\OutlookAttachments"

    Set ns = olApp.GetNamespace("MAPI")
    Set olFolder = ns.Folders("xyz@company.com").Folders("Inbox").Folders("Work in Progress (FC)")
    Set olInbox = ns.GetDefaultFolder(6)
    Set olFolderSub = olInbox.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
            Next j
            olMail.UnRead = False
            olMail.Move olFolderSub
        End If
    Next olMail

    Set olFolder = Nothing
    Set olItems = Nothing
    Set olMail = Nothing
    Set olApp = Nothing
End Sub

If you want to move ALL messages in the folder:

Private Sub CommandButton1_Click()
    Dim olApp As Object
    Dim olMail As Object
    Dim olItems As Object
    Dim ns As Object
    Dim olFolder As Object
    Dim olFolderSub As Object
    Dim olInbox As Object
    Dim j As Integer
    Dim Strfolder As String
    Dim FilePath As String

    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If

    Strfolder = "C:\Work Area\OutlookAttachments"

    Set ns = olApp.GetNamespace("MAPI")
    Set olFolder = ns.Folders("xyz@company.com").Folders("Inbox").Folders("Work in Progress (FC)")
    Set olInbox = ns.GetDefaultFolder(6)
    Set olFolderSub = olInbox.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
            Next j
        End If
        olMail.UnRead = False
        olMail.Move olFolderSub
    Next olMail

    Set olFolder = Nothing
    Set olItems = Nothing
    Set olMail = Nothing
    Set olApp = Nothing
End Sub

Hi @Hans Vogelaar 

 

Thanks for the code, but when I run the code, some of the mails are still in that folder.

 There were 7 mails in the folder, After I run the code, 3 remains in the folder as below image. 

Is there any problem with loop??

 

Sameer_Kuppanath_Sultan_0-1621246681803.png

 

@Sameer_Kuppanath_Sultan 

Does this work?

Private Sub CommandButton1_Click()
    Dim olApp As Object
    Dim olMail As Object
    Dim olItems As Object
    Dim ns As Object
    Dim olFolder As Object
    Dim olFolderSub As Object
    Dim olInbox As Object
    Dim i As Long
    Dim j As Integer
    Dim Strfolder As String
    Dim FilePath As String

    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If

    Strfolder = "C:\Work Area\OutlookAttachments"

    Set ns = olApp.GetNamespace("MAPI")
    Set olFolder = ns.Folders("xyz@company.com").Folders("Inbox").Folders("Work in Progress (FC)")
    Set olInbox = ns.GetDefaultFolder(6)
    Set olFolderSub = olInbox.Folders("Temp")
    Set olItems = olFolder.Items

    For i = olItems.Count To 1 Step -1
        Set olMail = olItems.Item(i)
        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
            Next j
        End If
        olMail.UnRead = False
        olMail.Move olFolderSub
    Next i

    Set olFolder = Nothing
    Set olItems = Nothing
    Set olMail = Nothing
    Set olApp = Nothing
End Sub
It doesn't delete the unread emails as far as I could understand. It has to be read in order to be considered as filed in the folder.
Mark all your e-mails as read and then run through the code, maybe it'll work :))

It's just an idea, but who am I? I just know that I don't know

Cheers
Nikolino

Hi @Hans Vogelaar 

Finally You saved my day.

 

Just to add it, if I want to delete the mails permanently instead of moving to "temp" folder,  could you please tell me what changes to be done in the code

 

olMail.Move olFolderSub

 

 

 

best response confirmed by Sameer_Kuppanath_Sultan (Frequent Contributor)
Solution

@Sameer_Kuppanath_Sultan 

Change that line to

olMail.Delete

This will move the email to your Deleted Items folder. You can set Outlook to automatically empty this folder when you quit the application, or you can add code to your macro to empty it.

Private Sub CommandButton1_Click()
    Dim olApp As Object
    Dim olMail As Object
    Dim olItems As Object
    Dim ns As Object
    Dim olFolder As Object
    Dim olFolderSub As Object
    Dim olInbox As Object
    Dim i As Long
    Dim j As Integer
    Dim Strfolder As String
    Dim FilePath As String

    Set olApp = GetObject(, "Outlook.Application")
    If Err.Number = 429 Then
        Set olApp = CreateObject("Outlook.Application")
    End If

    Strfolder = "C:\Work Area\OutlookAttachments"

    Set ns = olApp.GetNamespace("MAPI")
    Set olFolder = ns.Folders("xyz@company.com").Folders("Inbox").Folders("Work in Progress (FC)")
    Set olInbox = ns.GetDefaultFolder(6)
    Set olItems = olFolder.Items

    For i = olItems.Count To 1 Step -1
        Set olMail = olItems.Item(i)
        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
            Next j
        End If
        olMail.UnRead = False
        olMail.Delete
    Next i

    Set olFolder = ns.Folders("xyz@company.com").Folders("Inbox").Folders("Deleted Items")
    Set olItems = olFolder.Items
    For i = olItems.Count To 1 Step -1
        Set olMail = olItems.Item(i)
        olMail.Delete
    Next i

    Set olFolder = Nothing
    Set olItems = Nothing
    Set olMail = Nothing
    Set olApp = Nothing
End Sub

Hi @Hans Vogelaar 

 

Thanks a lot

 

"You are Awesome!'