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

Sdílet
Vložit
  • čas přidán 13. 09. 2024

Komentáře • 100

  • @pradeeprawatvlogs8358
    @pradeeprawatvlogs8358 Před 5 lety +8

    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🙏🙏🙏🙏🇮🇳

  • @davidjones5319
    @davidjones5319 Před 7 měsíci +1

    Wow. Awesome! Extremely valuable

  • @bilgehankavlak5330
    @bilgehankavlak5330 Před 3 lety +1

    Very clear explanations. Worthy informations. Great job.Thank you so much.

  • @PrasenjitSarkarSingapore
    @PrasenjitSarkarSingapore Před 4 lety +1

    Brilliant and highly relevant to our regular work! Thank you!

  • @Frenzy666
    @Frenzy666 Před 4 lety +1

    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  Před 4 lety

      Glad you found what you were looking for, thanks for watching!

  • @MasterChelsea
    @MasterChelsea Před 2 měsíci

    Sir, thank you a lot for this great video!

  • @johnmutethia3060
    @johnmutethia3060 Před 5 lety

    you are excelent! watching from KENYA

  • @liptonjh
    @liptonjh Před rokem +1

    This was super helpful. Thanks!

  • @abaiqbal
    @abaiqbal Před rokem +1

    Thank you so much Andrew

  • @kashifkhanspecial
    @kashifkhanspecial Před 5 lety +1

    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

  • @elevateddroneography247
    @elevateddroneography247 Před 3 lety +2

    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!

  • @vengion1379
    @vengion1379 Před 5 lety

    You are awesome. Thank you so much for these videos.

  • @abhisheksaraswat6753
    @abhisheksaraswat6753 Před 5 lety +3

    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.

  • @beckham7rick
    @beckham7rick Před 5 lety

    You have an amazing channel... thank u for sharing

  • @julianfolino
    @julianfolino Před 4 lety +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 Před rokem

      Could you find a solution to that? @wiseowltutorials

  • @senorrobinson
    @senorrobinson Před 4 lety

    Superb video! Thank you!

  • @jarodpeterson7830
    @jarodpeterson7830 Před 4 lety +1

    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?

  • @krn14242
    @krn14242 Před 5 lety

    Thanks Andrew, great stuff

  • @1526andrews
    @1526andrews Před 4 lety +1

    Hi Andrew, really useful video. Is there a line of code that I could to select attachments from emails sent on a certain date?

  • @lokeshvignesh7694
    @lokeshvignesh7694 Před 5 měsíci

    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

  • @inderjeetgehlot4480
    @inderjeetgehlot4480 Před 2 lety

    i am getting error the operation failed. An object could not be found

  • @irynamusiiovska828
    @irynamusiiovska828 Před 4 lety

    Thank you!

  • @marcelofreitas4593
    @marcelofreitas4593 Před 3 lety +1

    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  Před 3 lety +2

      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!

  • @michaelhoule4827
    @michaelhoule4827 Před 2 lety

    When I run it its gets stopped on the folder step. any idea Set fol = ns.Folders(1).Folders("Scans")

  • @murphy2870
    @murphy2870 Před 2 lety +1

    this works great
    can someone tell me how to do this for a specific outlook subfolder
    and
    for only excel attachments
    thanks

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 2 lety +1

      Hello! Video 29.6 in this playlist explains how to reference a specific Outlook folder czcams.com/play/PLNIs-AWhQzcleylKSN4MS-tJloReq0XcK.html
      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 Před 2 lety +1

      @@WiseOwlTutorials thanks

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 2 lety

      @@murphy2870 You're welcome, thanks for watching!

  • @harati6744
    @harati6744 Před rokem

    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?

  • @TheJaebeomPark
    @TheJaebeomPark Před 3 lety +1

    Awesome

  • @pannerselvammaruthachalam825

    Hi sir thank you so much can you give us same code in early binding method please

  • @SasiKumar-te1ir
    @SasiKumar-te1ir Před 5 lety

    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 Před 5 lety

      WiseOwlTutorials Thank you very much sir🤝...I will go through the above link now...however you will post a video for upcoming season.

  • @chrisholden6658
    @chrisholden6658 Před 3 lety +1

    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  Před 3 lety +1

      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 Před 3 lety

      @@WiseOwlTutorials thanks so much, you are a gentleman and a scholar, a tea and biccie donation coming your way :-)

  • @kendc4019
    @kendc4019 Před rokem

    Hi Andrew do you have the script for saving attachment and renaming it based on the subject of email?

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před rokem

      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!

  • @Nassuvian_Celtic
    @Nassuvian_Celtic Před 4 lety

    Where is the outlook folder saved ? I receive an object not found error on the set Folder line.

  • @janellchen6071
    @janellchen6071 Před 2 lety

    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  Před 2 lety

      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!

  • @beforedievisittheseplaces6369

    Getname space ("mapi") What is mapi i can't find previous definition of this please help

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 8 měsíci

      Hi! MAPI is WIndows' Messaging API en.wikipedia.org/wiki/MAPI
      I hope it helps!

  • @smhekma
    @smhekma Před 4 lety

    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

  • @sumeetpatil4785
    @sumeetpatil4785 Před 4 lety

    Code for collecting attachment from same subject mails..??

  • @joebarto
    @joebarto Před 3 lety

    I get a complier error saying ns variable not defined

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      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!)

  • @jansencosart1765
    @jansencosart1765 Před 2 lety

    how can i just pull the most recent email in folder

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 2 lety

      Hi Jansen, the answer here might help www.mrexcel.com/board/threads/get-only-the-latest-email-in-outlook-and-copy-to-excel.1088004/

  • @dmegnin
    @dmegnin Před 5 lety +1

    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 Před 5 lety

      @@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 Před 5 lety

      I'll try .Folders("Inbox").Folders("David")

    • @dmegnin
      @dmegnin Před 5 lety

      That did not work either. Nor did just .Folders("Inbox") I'll refer to the other videos you linked.

    • @dmegnin
      @dmegnin Před 5 lety

      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.
      czcams.com/video/tKNYgF2AKu0/video.html
      Thank you again for the great videos!

  • @9901028711
    @9901028711 Před rokem

    I need to save specific files (excel,word,pdf etc) to specific desktop folder specific subject line using macro code

  • @markvalenzuela7010
    @markvalenzuela7010 Před 3 lety

    what if i want to get the folder a from a different mailer account, ? thank you for this tutorials!

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      Hi Mark, we have another video which explains how to reference other accounts czcams.com/video/zgTqzDQ8VqI/video.html
      I hope it helps and thanks for watching!

    • @markvalenzuela7010
      @markvalenzuela7010 Před 3 lety +1

      @@WiseOwlTutorials wow thank you so much!

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      @@markvalenzuela7010 No problem!

  • @PankajNegi007
    @PankajNegi007 Před 2 lety

    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)

  • @markmiller4659
    @markmiller4659 Před 2 lety

    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  Před 2 lety

      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 Před 2 lety +1

      @@WiseOwlTutorials Thank you so much!!!!

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 2 lety

      @@markmiller4659 You're very welcome!

  • @HeroSanjA
    @HeroSanjA Před 2 lety

    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  Před 2 lety

      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!

  • @travllingisfun6189
    @travllingisfun6189 Před 3 lety

    Thanks for sharing.
    How to download only top 50 mail please suggest..

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety +1

      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 Před 3 lety +1

      @@WiseOwlTutorials Thank you very much.

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      @@travllingisfun6189 No problem!

  • @mohanmonu8293
    @mohanmonu8293 Před 3 lety

    Hello. How can we save attachmnets for specific email only rather than all mails from folder

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      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 czcams.com/play/PLNIs-AWhQzcleylKSN4MS-tJloReq0XcK.html
      I hope that helps!

  • @priyankamurugan4951
    @priyankamurugan4951 Před 3 lety

    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  Před 3 lety

      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

  • @TheRenzer9
    @TheRenzer9 Před 4 lety

    Set fol = ns.Folders(1).Folders("Clic Raw")
    Gives an error "object not found", once the system restarts. Why ?

    • @TheRenzer9
      @TheRenzer9 Před 4 lety +1

      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' 🙄

  • @prasenjitmandal5486
    @prasenjitmandal5486 Před 3 lety

    Hi, need your help
    Can we use macro in shared mail box

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 3 lety

      Hi, yes you can docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa220116(v=office.11)
      I hope it helps!

  • @AliDixon95
    @AliDixon95 Před 5 lety

    i got an error on the set fol line, i have no idea why?

    • @WiseOwlTutorials
      @WiseOwlTutorials  Před 5 lety

      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 czcams.com/video/tKNYgF2AKu0/video.html
      I hope it helps!

  • @gillianmcnosehair9097
    @gillianmcnosehair9097 Před 3 lety

    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  Před 3 lety

      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 Před 3 lety

      @@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  Před 3 lety

      @@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 Před 3 lety +1

      @@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  Před 3 lety +1

      @@gillianmcnosehair9097 Excellent! Happy to hear that you got it working, good stuff!

  • @bobbypetts9432
    @bobbypetts9432 Před 4 lety

    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?

  • @AbhisheakSaraswat
    @AbhisheakSaraswat Před 5 lety

    Awesome

  • @alexandrewilson5388
    @alexandrewilson5388 Před 3 lety

    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  Před 3 lety

      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!