Import Email Attachments from Outlook into Excel Automatically

Sdílet
Vložit
  • čas přidán 13. 09. 2024
  • How to import email attachments from Outlook into an Excel worksheet automatically using VBA. Here's the complete VBA code:
    Option Explicit
    Const AttachmentPath As String = "C:\myattachments\"
    Sub GetFromOutlook2()
    Dim outlookAtch As Object
    Dim NewFileName As String
    NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim OutlookMail As Variant
    Dim i As Integer
    Dim col As Long
    col = 0
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("CJ")
    i = 1
    For Each OutlookMail In Folder.Items
    If OutlookMail.Attachments.Count "GREATER THAN" 0 Then
    For Each outlookAtch In OutlookMail.Attachments
    If OutlookMail.ReceivedTime "GREATER THAN or EQUAL TO" = Range("start_Date").Value Then
    Range("email_Subject").Offset(i, 0).Value = OutlookMail.Subject
    Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime
    Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderName
    Range("email_Body").Offset(i, 0).Value = OutlookMail.Body
    outlookAtch.SaveAsFile NewFileName & outlookAtch.Filename
    Range("email_attachment").Offset(i, col).Value = outlookAtch
    col = col + 1
    End If
    Next outlookAtch
    col = 0
    i = i + 1
    End If
    Next OutlookMail
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing
    End Sub
    www.exceltrain...

Komentáře • 108

  • @juanpaolo21yt
    @juanpaolo21yt Před rokem

    This guy is so generous in sharing the actual code. I've been watching his videos since 2017. Thank you very much!!

  • @jamesmccormick9606
    @jamesmccormick9606 Před 4 lety

    Whenever I need to learn ANYTHING about Excel or VBA; your videos are ALWAYS my first choice when seeking help. One day would you mind making a video to help turn a worksheet created by someone that is a pivot table with merged rows for headers into an actual data set. I find that many of the customers I work with are unfamiliar with Excel and create pivot tables instead of data sets in stead of combining multiple datasets and creating a pivot table. Unfortunately they are unwilling to change their format because they have been doing it that way for so long. I wish there was an easier way.

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

    Sir I'm from Sri Lanka, your knowledge is very very useful and thank you so much

  • @genechicago4806
    @genechicago4806 Před 3 lety

    Mr. Takyar, your videos are very interesting and useful. I saw the first video you referenced before this one and it inspired me to something similar to this. You're a great teacher! Thank you!

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 3 lety

      Glad to hear that.

    • @genechicago4806
      @genechicago4806 Před 3 lety

      @@Exceltrainingvideos Hello, I modified your code to retrieve specific outlook attachments, renamed them to include the time stamp and saved them to different locations depending on what the report is for. I run the program daily and it was working good for a few months now. However, last Thursday when I ran the macro I received an error: "Run-time error '438': Object doesn't support this property or method."
      When I click on the debug button it highlights this line of code: If OutlookMail.ReceivedTime >= Range("start_Date").Value and OutlookMail.Subject = Range("Subject") And OutlookMail.Attachments.Count > 0 Then....
      I changed the declaration from Variant to Object fo the OutlookMail variable but I get the same error. I triple check the variables to they are all the same as I they were when the macro was working. Have you ever experience something like this? Does this macro still work for you?

  • @johnheffernan4879
    @johnheffernan4879 Před rokem

    Amazing. Just amazing. Thank you!!

  • @sergiomira741
    @sergiomira741 Před 4 lety

    Hi Sr... Greetings from Colombia... I love your videos, your Job is great!!!

  • @alvin3428
    @alvin3428 Před 2 lety +2

    Hi Sir, thank you for such a wonderful video. Is there a way that I can extract the specific content from the attachment (for example invoice PDF- Invoice no, Name, Date etc) that comes on a daily basis) in Excel? Can you please guide me through?

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

      czcams.com/video/rGLbrikWaLU/video.html
      czcams.com/video/9KJ0V3GdBm4/video.html

  • @Eric-qm4vm
    @Eric-qm4vm Před 2 lety

    Wonderful!!!! You provided the Code fir the Macro. Thank you very much. How can I get the excel sheet with the button assigned to the macro? I need this exact VBA Code and Excel combo. I too have subfolders in outlook. I am interested in extracting the same way. Thanks

  • @HuyenNguyen-bn2gr
    @HuyenNguyen-bn2gr Před rokem

    Thank you Sir. Great Video

  • @ericaleverson9430
    @ericaleverson9430 Před rokem

    I am new to using visual basics and not sure if I don't have something setup right and that is why I am running into this Global compile error it highlights this line of code
    If OutlookMail.ReceivedTime > = Range("start_Date").Value Then

  • @dibyendusharma7775
    @dibyendusharma7775 Před 4 lety

    Good evening sir,
    I'm Dibyendu Sharma from Tripura, India. I'm following your updates on CZcams for couple of years and got benefited in solving my day-to-day problems. Thank you so much sir.
    Sir, my question is "If it is possible to change the IP address of computer with a administrative password provided, with excel VBA programming"

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

      Will work on this. Are you trying to hack?

    • @dibyendusharma7775
      @dibyendusharma7775 Před 4 lety

      @@Exceltrainingvideos No sir,
      Actually I have two connections i.e., one for internet and other for intranet. And for this I need to change the IP address manually. I'm wondering if it could be automated.

  • @deenad6195
    @deenad6195 Před 2 lety

    I need to know how to get attachment in email find subject wise

  • @Maha-rw5yc
    @Maha-rw5yc Před rokem

    Hi Sir, Thanks for your wonderful explanation. Is there a way to get the content of an email attachment (which is an email). As I'm in Cyber Security field, we do recieve multiple phishing emails as an attachment for analysis. We are looking for a solution to extract the details of that attached emails.

  • @krn14242
    @krn14242 Před 4 lety

    Happy Birthday Dinesh. Thanks for the video. :)

  • @sanmaj100
    @sanmaj100 Před 4 lety

    Sir! You have saved my time. Thanks a lo.....t!

  • @mayurchawla89
    @mayurchawla89 Před rokem

    Hi Sir,
    Can we add the file size above or below 1mb or 2mb to be downloaded through an extractor??

  • @ndjanardhan
    @ndjanardhan Před 4 lety

    Thank you very much sir, y r the best in the vba. Sir one help for after attachment column there should be attachment hyperlink to open the file directly through excel. After all we should know the attachment of which mailbox or mail I'd. Thank you the help 😃.

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

      Excellent idea. This link will help: www.exceltrainingvideos.com/automate-invoice-report-generation-using-excel-vba/

  • @shaheenaisitok
    @shaheenaisitok Před 4 lety

    Hello sir,
    I am the regular viewer of your videos, it is very help ful for me,,
    Recently i watched your video regarding attachment from outlook,
    I tried but stuck in middle , i am having compile error, user- defined type not defined how to resolve this pls suggest

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      Just activate the Microsoft Object Library as shown in this video: czcams.com/video/R0qzfYJr6d8/video.html

  • @sheharakalansuriya4100

    Hi Dinesh It worked fine to me, but it is not showing the emails without attachments

  • @sarojinimanika4675
    @sarojinimanika4675 Před rokem

    Thank you

  • @vincepardilla1135
    @vincepardilla1135 Před 2 lety

    How to download attachment with same filenames (ex. 3 different emails sent in different days with 3 same attached files/filenames). your code works but it only produce 1 file in the folder (the first one was downloaded but it replaces the other 2 files). please help. Thanks!

  • @mohansrinivasanv.5448
    @mohansrinivasanv.5448 Před 4 lety

    Also, instead of a constant folder in the code, I would like to choose the destination folder, each time, where the attachments will be stored. How this code will then change? Thank you for your help.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      This VBA tutorial link will help: www.exceltrainingvideos.com/how-to-loop-through-subfolders/
      You can also search my CZcams channel or website: www.exceltrainingvideos.com

  • @SaurabhKumar-yt8di
    @SaurabhKumar-yt8di Před 4 lety

    Hi Sir,
    Thank u sir, All your videos are so wonderful. I have a query, I want to get count of attachment in a column that too only excel attachment.. When I do Outlookmail. attachments. count, It also gives me count of embedded image count that is in signature of email.. Please help

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      Place a counter variable in the inside loop to count the number of attachments quickly and easily. This link will guide: www.exceltrainingvideos.com/counting-and-displaying-totals-automatically-using-excel-vba/

  • @jessereyes5009
    @jessereyes5009 Před 2 lety

    Sir i have send one excel file to all my stores to fill the details. They all replied the same excel file with same name. When i run the above vba code the attachment in the my attachment forder is only one showing because the file name was same for all replied attachment. Can u plz help me.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 2 lety

      Ask the stores to add a store name to the file because files with the same name cannot exist in the same folder!

  • @vittal255
    @vittal255 Před 4 lety

    Sir, I have been following your videos since 2013, I was developing a small application for my office. Presently I am working on the project in which I need the msg to pop up and close itself after certain seconds.

  • @mohansrinivasanv.5448
    @mohansrinivasanv.5448 Před 4 lety

    Sir, How to add email ids of To, CC and BCC against each? What would be the additional lines of code, please? Thanks in advance for your kind help.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      This VBA solution will help: www.exceltrainingvideos.com/how-to-use-automation-to-send-ms-outlook-mail-using-excel-vba/
      You can also search my channel or website www.exceltrainingvideos.com

  • @sumithrab4260
    @sumithrab4260 Před 2 lety

    Hi Sir, Your video is very useful . Please upload a VBA sample file also.
    Thanks Sir.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 2 lety

      www.exceltrainingvideos.com/tag/import-email-attachments-from-outlook-into-excel-automatically/

  • @lowwilson7513
    @lowwilson7513 Před 4 lety

    Hi. Very awesome video. Can you share how if I wanna to get attachment from non defaulted mailbox?

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

      Yes, sure. This VBA tutorial will guide: czcams.com/video/R0qzfYJr6d8/video.html

    • @lowwilson7513
      @lowwilson7513 Před 4 lety

      @@Exceltrainingvideos thank!

  • @gouravbhakat
    @gouravbhakat Před 3 lety

    how to select folders from a different mailbox other than the default mailbox if you have multiple mailboxes configured in outlook?

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 3 lety

      This Excel VBA tutorial will help: czcams.com/video/R0qzfYJr6d8/video.html

  • @ahmedhassanahmed6263
    @ahmedhassanahmed6263 Před 4 lety

    thank you for your great videos. kindly i want to print a userform in a landscape view. i write userform1.printform but it prints it as a portrait. how to solve this? thank you

  • @thandazanigazu4986
    @thandazanigazu4986 Před 3 lety

    Hi Dinesh, I am getting this error when I am trying to run this code "Error Number: -2147221233, the attempted operation failed. An object could not be found vba" what could be the problem, I did references.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 3 lety

      This link will help: www.exceltrainingvideos.com/tag/import-email-attachments-from-outlook-into-excel-automatically/
      Check your code line by line.

  • @tiatyot
    @tiatyot Před 4 lety

    Thank you so much, Sir :)

  • @Altoteam
    @Altoteam Před 4 lety

    Hi Sir,
    I’m having some trouble trying to get some data from outlook.
    In this case the received email contains text as titles and excel grids pasted on the body with more information contained.
    When I run the Visual Basic I only get the titles.
    Could you help me please?
    Thanks in advanced and Regards from Argentina.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      Check you code. This link will guide: www.exceltrainingvideos.com/tag/import-email-attachments-from-outlook-into-excel-automatically/

  • @SunilChavan-dp4in
    @SunilChavan-dp4in Před 2 lety

    Sir, First time I am watching the video and something like this .. I have copied all the codes. Somehow I am getting this
    runtime error 438 object doesn't support this property . Please help how to fix the error

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 2 lety

      If you copy/paste the codes, they will not work. It is better to copy the code into notepad, check for errors like change in characters. Finally, use the code in your VB editor.

  • @MindfulMinutes2309
    @MindfulMinutes2309 Před 3 lety

    How to get emails from different mailbox accounts to excel

  • @asad811
    @asad811 Před 4 lety

    When the code gets to the following line we get an run-time error (attached picture) and we have searched the net but nothing usful was found to solve the problem.
    Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders

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

      Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders("CJ")
      Read the details in the description.
      What should your line of code do?

  • @iamriza5486
    @iamriza5486 Před 4 lety

    hi Dinesh. thank you for this video. I am having a problem. When I change the date like July 29 onwards, outlook emails are not exporting to excel. But when I place July 28 it is working. July 28 is the date I made the outlook folder. What is the probable cause of this? Thank you

  • @kathik5240
    @kathik5240 Před 2 lety

    Sir I am facing a problem it takes my entire day time of mine .. I send a mail to sales to get feedback from them like I send to 40ppl and they reply individual with same attachment 40 times I want something which automatically collect those attachment and make it one sheet .. pls reply even if such this is not positive

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 2 lety

      That's what you just learnt in the video!

    • @kathik5240
      @kathik5240 Před 2 lety

      @@Exceltrainingvideos sir I wany like once a mail comes with attached tht attachments needs get collated and get one Excel sheet

  • @jielynsanmiguel6732
    @jielynsanmiguel6732 Před 3 lety

    I have problem on
    If OutlookMail.ReceivedTime >= Range(“start_Date”).Value
    it shows syntax error

  • @ankkitgarg55
    @ankkitgarg55 Před 4 lety

    Hi sir, My previous code is working now, but in this attachment code there is nothing coming up in the excel and no error is displayed ?

  • @kjoshua37
    @kjoshua37 Před 3 lety

    How do I separate the file type? Like if I want PDFs to go to one folder then spreadsheets to go to another?!

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 3 lety

      This Excel VBA tutorial will help: www.exceltrainingvideos.com/copy-specific-files-from-folder-and-subfolders-into-destination-folder/
      Alternatively, you can search my website using the Google search-box.

  • @winniekang9770
    @winniekang9770 Před 4 lety

    Hi Sir, I got an error message "Complie error: User-defined type not defined" and the VBA command lines were highlighted at 'Dim OutlookApp As Outlook.Application" and "Sub GetFromOutlook2(). May i know how to rectify this ?

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      Try again. You haven't referenced the Microsoft Outlook Library. This link will guide: www.exceltrainingvideos.com/send-emails-from-specific-account-in-outlook/

  • @alpeshshah6342
    @alpeshshah6342 Před 3 lety

    Respected Sir, How to save outlook attachment contents in excel through VbA code ? As I want code to open email attachment and extract that content in excel.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 3 lety

      This line of code will guide: Workbooks.Open Filename:=Range(“A1”)

  • @maharajnarayanan1060
    @maharajnarayanan1060 Před 4 lety

    Hi Sir, Is there any way i can bring the content of the email attachment next to the Body of the message column in excel, since many of my logs are in attachment?

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      This link will help: czcams.com/video/9KJ0V3GdBm4/video.html

    • @maharajnarayanan1060
      @maharajnarayanan1060 Před 4 lety

      Dinesh Kumar Takyar hi sir, thank you very much for the swift response, but to parse the content of body of the email I have already done with excel formula, my issue is some of my logs comes in attachment as text file. From one of your other video I get to know how to save them in drive, but is there a way to read the content of the attachment and parse as the the same with the link u have shared??

    • @manoelitonunes855
      @manoelitonunes855 Před 3 lety

      @@maharajnarayanan1060 I know it's been a long time you posted that and didn't receive a response. I am facing a very similar situation. Did you find the solution? are u able to share please? Thanks

  • @rutujanakure4581
    @rutujanakure4581 Před 3 lety

    Hii sir , I want to download an excel from outlook and then sort data automatically using VBA .how to do this ? please help

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

      This link will guide: czcams.com/video/5KstpYYvC80/video.html
      You can also search my channel.

    • @rutujanakure4581
      @rutujanakure4581 Před 3 lety

      @@Exceltrainingvideos Thank you sir

  • @ayushbhujel
    @ayushbhujel Před 4 lety

    Sir i would request to provide code instead of file name of attachment ,plz provided link for file so that we can click and open the respective attachment.

  • @dharmeshranade4789
    @dharmeshranade4789 Před 3 lety

    Sir, First time I am watching the video and something like this .. I have copied all the codes. Somehow I am getting this error Compile error: User-defined type not defined on on 5th line after Const code.. Please help how to fix the error

  • @sherifmudavan
    @sherifmudavan Před 4 lety

    Sir, how can I download email attachments which is received on particular date

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      This VBA tutorial will help: czcams.com/video/4xJagIwUWoY/video.html
      Do you know how to do a search? Use your skills!

    • @sherifmudavan
      @sherifmudavan Před 4 lety

      Sir, provided link is same as above

  • @ayushbhujel
    @ayushbhujel Před 4 lety

    sir if the attachment name is same in two different mails then it's replacing the previous attachment. So plz suggest me how to get rid of it.

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      What have you tried?

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

      @@Exceltrainingvideos
      Sir i have send one excel file to all my stores to fill the details. They all replied the same excel file with same name. When i run the above vba code the attachment in the my attachment forder is only one showing because the file name was same for all replied attachment. Can u plz help me.

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

      @@ayushbhujel I'll make a video on this interesting solution soon.

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

      @@Exceltrainingvideos Thank you sir

  • @maximilianchavez3578
    @maximilianchavez3578 Před 4 lety

    Sir may i request a inventory excel vba program for my furniture welding shop like steel bed, dining table and chair. all are using steel tube or pipe. from cutting of raw pipe tube to welding by parts of steel bed like headboard, footboard, bed flooring, supports, etc. then powdercoating paint company pickup the steel bed
    parts/set. then after 2 days deliver back to us. but my ....
    first problem is the monitoring, when they deliver back not complete in parts. after they complete the parts, we deliver to our client furniture store. .....
    My second problem is the monitoring of payment, 60days term, 90days, 120days before they pay us, when they pay balance only, not full payment. this monitoring payment i have a problem also.....
    My third problem is the raw material costing, from raw material pipe like 2" x 20ft round tube, 1"x2" x 20ft square tube to make steel bed frame. can i send picture of our welding shop job? im not well in english grammar. i want to send picture to you our welding shop transactions. Please!

    • @Exceltrainingvideos
      @Exceltrainingvideos  Před 4 lety

      Please search for inventory on my website: www.exceltrainingvideos.com/