Тёмный

Excel VBA Introduction Part 29.8 - Saving Attachments from an Outlook Folder 

WiseOwlTutorials
Подписаться 208 тыс.
Просмотров 33 тыс.
50% 1

By Andrew Gould
www.wiseowl.co.uk - This video explains how to save the attachments from a collection of emails in an Outlook folder. You'll learn how to identify which emails have attachments, how to loop over the attachments collection and how to use the SaveAsFile method. You'll also see how to use a FileSystemObject to create a new folder for each email's attachments collection.
You can download the completed file here www.wiseowl.co.uk/vba-macros/...
If you'd like to help fund Wise Owl's conversion of tea and biscuits into quality training videos, you can click this link www.wiseowl.co.uk/donate?t=1 to make a donation. Thanks for watching!
You can buy our Introduction to Excel VBA book here www.lulu.com/shop/andrew-gould...
Visit www.wiseowl.co.uk for more online training resources in Microsoft Excel, SQL Server, Reporting Services, Analysis Services, Integration Services, ASP.NET, C#, Visual Basic, Microsoft Access, Microsoft PowerPoint, Microsoft Word, Microsoft Project, Microsoft Publisher, Microsoft Visio and more

Опубликовано:

 

6 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 100   
@PrasenjitSarkarSingapore
@PrasenjitSarkarSingapore 4 года назад
Brilliant and highly relevant to our regular work! Thank you!
@johnmutethia3060
@johnmutethia3060 5 лет назад
you are excelent! watching from KENYA
@pradeeprawatvlogs8358
@pradeeprawatvlogs8358 5 лет назад
Thank you so much sir for giving us such a valuable knowledge without any charges with the help of you we can boost our career and knowledge too🙏🙏🙏🙏🇮🇳
@pradeeprawatvlogs8358
@pradeeprawatvlogs8358 5 лет назад
You’re my favourite sir
@bilgehankavlak5330
@bilgehankavlak5330 3 года назад
Very clear explanations. Worthy informations. Great job.Thank you so much.
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Thank you Bilgehan!
@vengion1379
@vengion1379 5 лет назад
You are awesome. Thank you so much for these videos.
@davidjones5319
@davidjones5319 5 месяцев назад
Wow. Awesome! Extremely valuable
@WiseOwlTutorials
@WiseOwlTutorials 5 месяцев назад
Thanks for watching, David!
@beckham7rick
@beckham7rick 5 лет назад
You have an amazing channel... thank u for sharing
@krn14242
@krn14242 5 лет назад
Thanks Andrew, great stuff
@kashifkhanspecial
@kashifkhanspecial 5 лет назад
Hi Andrew, Thank you so much for the tutorial, now I have the idea that how can I play with the attachments. Thanks once again. Thanks Kashif
@senorrobinson
@senorrobinson 4 года назад
Superb video! Thank you!
@MasterChelsea
@MasterChelsea 28 дней назад
Sir, thank you a lot for this great video!
@Frenzy666
@Frenzy666 4 года назад
This video is the only thing that actually helped me save all attachments, every other tutorial on the web is kinda whack... couldnt get them to work. This vid is perfect. I also like that you actually explain the code as you write it.
@WiseOwlTutorials
@WiseOwlTutorials 4 года назад
Glad you found what you were looking for, thanks for watching!
@liptonjh
@liptonjh Год назад
This was super helpful. Thanks!
@WiseOwlTutorials
@WiseOwlTutorials Год назад
Cheers Jeff, happy to hear it helped!
@abaiqbal
@abaiqbal Год назад
Thank you so much Andrew
@WiseOwlTutorials
@WiseOwlTutorials Год назад
You're very welcome Aba, thank you for watching!
@irynamusiiovska828
@irynamusiiovska828 4 года назад
Thank you!
@jarodpeterson7830
@jarodpeterson7830 4 года назад
These videos are amazing! Thank you for sharing your knowledge. One question I have is will this code, when ran every time, re-save old files and cause duplicates? If so if there a line of code to insert that will pull all files once and then only pull the latest email received?
@1526andrews
@1526andrews 3 года назад
Hi Andrew, really useful video. Is there a line of code that I could to select attachments from emails sent on a certain date?
@elevateddroneography247
@elevateddroneography247 3 года назад
Andrew great video, even I could get it to work! I do have one question. Is there a way for the macro to run automatically with Outlook open? What code do I need to add so I don’t have to run the macro...... Thanks!
@abhisheksaraswat6753
@abhisheksaraswat6753 5 лет назад
Hi Andrew, How we can download the attachment from the particular email or specific time period. for example: Someone share the excel files from last 2 month, and i want to save all the attachment of that specific email id for only last month. It can be possible. if Yes please help us.
@julianfolino
@julianfolino 4 года назад
Hi! I'm having a bug when I want to test it in Immediate (min: 3:55) the error appears to be in Set fol = ns.Folders(1).Folders("Foldername") Do you know what could be it?
@AN-qi6ye
@AN-qi6ye Год назад
Could you find a solution to that? @wiseowltutorials
@pannerselvammaruthachalam825
@pannerselvammaruthachalam825 5 лет назад
Hi sir thank you so much can you give us same code in early binding method please
@AbhisheakSaraswat
@AbhisheakSaraswat 5 лет назад
Awesome
@SasiKumar-te1ir
@SasiKumar-te1ir 5 лет назад
Thank you very much sir🙏🏻I’ve learned a lot...could you please help to make a video for PDF macros using VBA (pull specific data from pdf and paste into excel)
@SasiKumar-te1ir
@SasiKumar-te1ir 5 лет назад
WiseOwlTutorials Thank you very much sir🤝...I will go through the above link now...however you will post a video for upcoming season.
@sumeetpatil4785
@sumeetpatil4785 4 года назад
Code for collecting attachment from same subject mails..??
@Nassuvian_Celtic
@Nassuvian_Celtic 4 года назад
Where is the outlook folder saved ? I receive an object not found error on the set Folder line.
@janellchen6071
@janellchen6071 2 года назад
hi there, thank you so much for the knowledge. Is there a way we can replace special characters in the email subject, such as (*/>,
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hi! You can use the Replace function to do this docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/replace-function I hope it helps!
@lokeshvignesh7694
@lokeshvignesh7694 4 месяца назад
Hi, i tried and I'm getting an error stating object could not be found for Folders which I set also the folder is available on outlook
@michaelhoule4827
@michaelhoule4827 2 года назад
When I run it its gets stopped on the folder step. any idea Set fol = ns.Folders(1).Folders("Scans")
@harati6744
@harati6744 Год назад
Hi there! I follow th macro but when i try to access to my Inbox sub folder "Test" i don't get to it - i receive Run-time error - 2147221233 The attempted operation failed. An object could not be found. But my sub folder Test is there. How can i avoid that error?
@marcelofreitas4593
@marcelofreitas4593 3 года назад
Hello, Andrew! Amazing job! Is there a way to unzip a file using VBA? Everyday, I receive a zip file in the Outlook. So, I need to unpack it manually to a folder and after that I use a VBA routine to manipulate the data. I would like to automatize the whole process. Can you help me? Thanks a lot for share your knowleadge.
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Thanks Marcelo! Here's the basic code to unzip a folder into a separate existing folder: Dim sh As Object Set sh = CreateObject("Shell.Application") sh.Namespace(ThisWorkbook.Path & "\Unzipped Files").CopyHere _ sh.Namespace(ThisWorkbook.Path & "\Zipped Files.zip").Items And if you'd like to know what it all means, watch out for a video appearing soon!
@inderjeetgehlot4480
@inderjeetgehlot4480 2 года назад
i am getting error the operation failed. An object could not be found
@chrisholden6658
@chrisholden6658 3 года назад
thanks for this it was really helpful. Can I point this to a windows folder where the outlook items are located instead of outlook itself? I receive an email with a zipfile attached. I have to unzip the contents of that file which has 30 days worth of emails each with an attachment. I can copy and paste them back in an outlook folder and run this but ideally would like to be able to run it against the windows folder. Thanks
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Chris! Yes, you can loop through email items stored in a Windows folder. There are several ways to do this, here's a simple one using the Dir function. It assumes that you have a folder called Emails containing the saved messages and a folder called Attachments in the same folder as the Excel file which contains your VBA code: Sub SaveAttachmentsFromFileSystem() Dim AttachmentsFolder As String Dim EmailFolder As String Dim EmailPath As String Dim olk As Outlook.Application Dim mi As Outlook.MailItem Dim at As Outlook.Attachment EmailFolder = ThisWorkbook.Path & "\Emails\" EmailPath = Dir(EmailFolder & "*.msg") If EmailPath = "" Then Exit Sub AttachmentsFolder = ThisWorkbook.Path & "\Attachments\" Set olk = New Outlook.Application Do Debug.Print EmailPath Set mi = olk.CreateItemFromTemplate(EmailFolder & EmailPath) mi.Display For Each at In mi.Attachments Debug.Print at.Filename at.SaveAsFile AttachmentsFolder & at.Filename Next at mi.Close olDiscard EmailPath = Dir Loop Until EmailPath = "" End Sub
@chrisholden6658
@chrisholden6658 3 года назад
@@WiseOwlTutorials thanks so much, you are a gentleman and a scholar, a tea and biccie donation coming your way :-)
@murphy2870
@murphy2870 2 года назад
this works great can someone tell me how to do this for a specific outlook subfolder and for only excel attachments thanks
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hello! Video 29.6 in this playlist explains how to reference a specific Outlook folder ru-vid.com/group/PLNIs-AWhQzcleylKSN4MS-tJloReq0XcK When you loop through attachments you can test if the final four characters of the FileName property equals "xlsx" to determine if it's an Excel file (adjust for xlsm or xlsb files) If LCase(Right(atc.FileName, 4)) = "xlsx" Then
@murphy2870
@murphy2870 2 года назад
@@WiseOwlTutorials thanks
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
@@murphy2870 You're welcome, thanks for watching!
@kendc4019
@kendc4019 Год назад
Hi Andrew do you have the script for saving attachment and renaming it based on the subject of email?
@WiseOwlTutorials
@WiseOwlTutorials Год назад
Hi Ken! Did you mean you wanted the code shown in the video? If so you can find a link in the video description to download the completed file. I hope it helps and thanks for watching!
@prasenjitmandal5486
@prasenjitmandal5486 3 года назад
Hi, need your help Can we use macro in shared mail box
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi, yes you can docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa220116(v=office.11) I hope it helps!
@markvalenzuela7010
@markvalenzuela7010 3 года назад
what if i want to get the folder a from a different mailer account, ? thank you for this tutorials!
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Mark, we have another video which explains how to reference other accounts ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-zgTqzDQ8VqI.html I hope it helps and thanks for watching!
@markvalenzuela7010
@markvalenzuela7010 3 года назад
@@WiseOwlTutorials wow thank you so much!
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
@@markvalenzuela7010 No problem!
@HeroSanjA
@HeroSanjA 2 года назад
Hello, can you please tell ne how to download only e-mail, which have the word "gesamt" in the attachment name ? Thank you:)
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hi Alex! You can use the InStr function docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/instr-function or the Like operator docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/like-operator to check if the attachment name contains a particular string. I hope that helps!
@smhekma
@smhekma 4 года назад
Guessing there has to be an easy solution, but I can't figure out what I would need to add if I just wanted to save files that have some unique criteria in the file name. So in this section I want to add what I have in parenthesis in the first line, but I can't figure out how to translate this into VBA. For Each at In mi.Attachments (That includes "Calls and Transfers in at.Filename) 'Debug.Print vbTab, at.DisplayName, at.FileName, at.Size at.SaveAsFile "Y:\STATS\Processing\" & Format(mi.ReceivedTime, "yyyy-mm-dd hh-nn-ss ") & at.FileName
@alexandrewilson5388
@alexandrewilson5388 3 года назад
Hello, I get a runtime error 76, Path not found at line "Set dir = fso.CreateFolder(dirName)". I could retrieve and save the attachments so everything was running fine until I got to the stage of creating folders for the attachments. Any idea what caused the error?
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Alexandre! There are a couple of possible reasons for this. One is that you haven't created all of the necessary parent folders - the CreateFolder method can only create one level at a time. Another possibility is a simple spelling error in the folder path of the value in your dirName variable. I hope that helps!
@travllingisfun6189
@travllingisfun6189 3 года назад
Thanks for sharing. How to download only top 50 mail please suggest..
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi there, here's one solution stackoverflow.com/questions/52927919/top-50-emails-from-outlook-from-new-to-old I hope it helps!
@travllingisfun6189
@travllingisfun6189 3 года назад
@@WiseOwlTutorials Thank you very much.
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
@@travllingisfun6189 No problem!
@mohanmonu8293
@mohanmonu8293 3 года назад
Hello. How can we save attachmnets for specific email only rather than all mails from folder
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Mohan! It depends on how you're identifying the specific email - you could use filters with the Find or Restrict methods to do this as described in videos 29.9 and 29.13 in this playlist ru-vid.com/group/PLNIs-AWhQzcleylKSN4MS-tJloReq0XcK I hope that helps!
@TheRenzer9
@TheRenzer9 4 года назад
Set fol = ns.Folders(1).Folders("Clic Raw") Gives an error "object not found", once the system restarts. Why ?
@TheRenzer9
@TheRenzer9 4 года назад
Also to bring to your notice that this issue occurs only when I reboot my system and launch outlook again after a system reboot. Also i tried something new. When I rebooted my system this time I knew I would get the error again and so I did as expected..but in order to fix it all I did was to enter '2' in Folders (1) instead of '1'...and it worked...to be sure I restarted my system again with '2' in place this time...and I got the same error...so this time in order to fix it i replaced '2' back with '1' and it worked like a charm again... My new question after this tiny experiment is...why can't we simply set a presidency for the non-default folder once and for all rather than its presidency switching back and forth between 1 and 2...who knows after the next reboot I might have to use '3' 🙄
@dmegnin
@dmegnin 5 лет назад
Great video. Nice clear instructions. Thank you! I followed them very carefully and I'm getting "An object could not be found" Run-time error on the Set fol = ns.Folders(1).Folders("David") line. I did add the reference to the Microsoft Outlook and double checked it. I checked the folder name. What could be the problem? Option Explicit Sub SaveOutlookAttachements() Dim ol As Outlook.Application Dim ns As Outlook.Namespace Dim fol As Outlook.Folder Dim i As Object Dim mi As Outlook.MailItem Set ol = New Outlook.Application Set ns = ol.GetNamespace("MAPI") Set fol = ns.Folders(1).Folders("David") For Each i In fol.Items If i.Class = olMail Then Set mi = i Debug.Print mi.SenderName, mi.ReceivedTime End If Next i End Sub
@dmegnin
@dmegnin 5 лет назад
@@WiseOwlTutorials Thank you. I'll try that. I did think of that, but it looked like your "Dell" folder was a subfolder also, so I didn't think it would matter. My "David" folder is only a subfolder of "Inbox." Is that considered the root level?
@dmegnin
@dmegnin 5 лет назад
I'll try .Folders("Inbox").Folders("David")
@dmegnin
@dmegnin 5 лет назад
That did not work either. Nor did just .Folders("Inbox") I'll refer to the other videos you linked.
@dmegnin
@dmegnin 5 лет назад
Well, I guess our organization is kind of odd. This is what worked for my Outlook folder structure: Set fol = ns.Folders(2).Folders("Inbox").Folders("David") Yeah, Folders(2) This video was extremely helpful. Thanks for the link to it. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-tKNYgF2AKu0.html Thank you again for the great videos!
@PankajNegi007
@PankajNegi007 2 года назад
Dear Sir, I had a query regarding VBA Code Attaching Active Excel workbook in outlook Sir I made a VBA Program where i copy and paste data in new workbook now i want to attached this new workbook in outlook file using "Application.Dialogs(xlDialogSendMail).Show" this Technique. Now when i run this macro in Step Into mode[F8] its working fine but when i run same macro Run mode(F5/Green Triangle Button) taking lots of time and showig this errors messages "cannot find this file verify the path and file name are correct". So Sir Please Help me on this matter. Sir if possible please Make vedio on this Topic (Application.Dialogs(xlDialogSendMail).Show)
@AliDixon95
@AliDixon95 5 лет назад
i got an error on the set fol line, i have no idea why?
@WiseOwlTutorials
@WiseOwlTutorials 5 лет назад
Hi Ali, another viewer reported that using ns.Folders(2).Folders("Inbox").Folders("Dell") was required to locate the correct folder for him. You might find this video useful ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-tKNYgF2AKu0.html I hope it helps!
@priyankamurugan4951
@priyankamurugan4951 3 года назад
I get an error " you dont have appropriate permission to perform this operation" when i try to save the attachment in my local folder. how do i tackle this?
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Priyanka, it seems that this can be caused by a variety of reasons. My suggestion would be to rely on Google (something like "outlook attachment appropriate permission to perform this operation" will do) and work your way through the suggested solutions
@beforedievisittheseplaces6369
@beforedievisittheseplaces6369 7 месяцев назад
Getname space ("mapi") What is mapi i can't find previous definition of this please help
@WiseOwlTutorials
@WiseOwlTutorials 7 месяцев назад
Hi! MAPI is WIndows' Messaging API en.wikipedia.org/wiki/MAPI I hope it helps!
@markmiller4659
@markmiller4659 2 года назад
How can this be automated when receiving a new email with an attachment in a subfolder I specify ? It works great when I run it manually but I can't get it to run when I receive a new email with an attachment. Thanks in advance!!! Your videos are awesome!!!
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hi Mark! You can use the NewMail event of the Outlook application to trigger code when mail is received docs.microsoft.com/en-us/office/vba/api/outlook.application.newmail I hope it helps!
@markmiller4659
@markmiller4659 2 года назад
@@WiseOwlTutorials Thank you so much!!!!
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
@@markmiller4659 You're very welcome!
@jansencosart1765
@jansencosart1765 2 года назад
how can i just pull the most recent email in folder
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hi Jansen, the answer here might help www.mrexcel.com/board/threads/get-only-the-latest-email-in-outlook-and-copy-to-excel.1088004/
@joebarto
@joebarto 3 года назад
I get a complier error saying ns variable not defined
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Hi Joe, check that you have a Dim statement for the ns variable and that you've spelt it correctly (that sounds patronising but I've lost count of how many times I've misspelt a simple variable name and seen this compile error!)
@9901028711
@9901028711 11 месяцев назад
I need to save specific files (excel,word,pdf etc) to specific desktop folder specific subject line using macro code
@gillianmcnosehair9097
@gillianmcnosehair9097 2 года назад
What a cracking set of videos! Thank you so much for this Andrew! I am trying to amend code so it only downloads attachments AFTER 05/09/21 and the file size is over 15KB. When I run the routine the date aspects appears to work but the file cap is ignored. Could someone please help? Is it best to filter via "IF" or restrict method or a combo of both. Thanks so much. Option Explicit Sub SaveOutlookAttachments() Dim olApp As Outlook.Application Dim olNS As Outlook.Namespace Dim olFolder As Outlook.Folder Dim olItem As Object Dim mi As Outlook.MailItem Dim olAtt As Outlook.Attachment Dim filterstring As String Set olApp = New Outlook.Application Set olNS = olApp.GetNamespace("MAPI") Set olFolder = olNS.PickFolder For Each olItem In olFolder.Items If olItem.Class = olMail Then Set mi = olItem If mi.Attachments.Count > 0 And mi.ReceivedTime > "5 / 9 / 2021" And mi.Size > 15000 Then Debug.Print mi.SenderName, mi.ReceivedTime, mi.Attachments.Count, mi.Size For Each olAtt In mi.Attachments olAtt.SaveAsFile "C:\Users\John.Doe\Pictures\OUTLOOKEXPORT\Attachments Export" & Format(mi.ReceivedTime, "yyyy-mm-dd hh-nn-ss ") & olAtt.Filename Next olAtt End If End If Next olItem End Sub
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
Hi Gillian, the test for size of the mail item looks fine and should work - it does for me. Bear in mind that the Size property of the mail item represents the full size of the entire email. If you want to check the size of each attachment separately, test the Size property of your olAtt variable in the second loop. I hope it helps!
@gillianmcnosehair9097
@gillianmcnosehair9097 2 года назад
@@WiseOwlTutorials Thanks so much for getting back to me. When I run the routine based on current parameters, I still pick up 5KB PNG files which is wrong. I have tried to add "For Each olAtt In mi.Attachments.Restrict(Filterstring)" But I do not have the intellisense drop-down option in the second loop? I have added Filterstring as a variable, and although it is not included in the earlier code, I would add FilterString = "[size] > 15000 AND [receivedtime] > '05/09/2021'" just above the first "For Each" loop. Would love your help on this.
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
@@gillianmcnosehair9097 You can't apply the Restrict method to the object returned by the Attachments property. Just loop through the attachments as you showed in the original code you posted. Add an If statement inside this loop which tests the olAtt.Size property, not the mi.Size property. I hope it helps!
@gillianmcnosehair9097
@gillianmcnosehair9097 2 года назад
@@WiseOwlTutorials Top, Top man. Thanks so much, Andrew. I added the following code: "For Each olAtt In mi.Attachments If olAtt.Size > 10000 And mi.ReceivedTime > "5/09/2021" Then" and it recognised the "IF" and executed perfectly. I've been toiling for an answer for weeks! Just stumbled upon your content and I've acquired a mass of knowledge from your excellent videos. I would like to thank you for making them available.
@WiseOwlTutorials
@WiseOwlTutorials 2 года назад
@@gillianmcnosehair9097 Excellent! Happy to hear that you got it working, good stuff!
@bobbypetts9432
@bobbypetts9432 4 года назад
I am having an issue when I loop through a folder (for each i in fol.items), only about half of the emails are being picked up by the macro. I've tried setting fol.items to an Outlook.Items object and sorting, but still no good. Do you have any idea what could be happening here?
@akankshagupta9196
@akankshagupta9196 3 года назад
Hi bobby
@TheJaebeomPark
@TheJaebeomPark 3 года назад
Awesome
@WiseOwlTutorials
@WiseOwlTutorials 3 года назад
Thanks!
Далее
Пиратские котики
00:50
Просмотров 109 тыс.
VBA Unleashed: Send Customized Emails in One Line
11:10