+ Reply to Thread
Results 1 to 14 of 14

Opening .Xls Attachement from Email based on Subject/Sender

  1. #1
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Opening .Xls Attachement from Email based on Subject/Sender

    Hello guys,

    I have a the following script that will Open an Excel Attachment from a selected email in Outlook. I am trying to automate it so the user doesn't actually have to click/select the email in Outlook before running. I receive an email in my Inbox everyday Sent by the same user "[email protected]" AND Subject "Open Trades", the email will always have an excel workbook as attachment.

    Can I edit this script so that it searches for the newest email sent today and open it? without me having to select it?



    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Forgot to mention, if you have more than one email account configured in Outlook, you'll have to change this line accordingly:
    Please Login or Register  to view this content.
    Tim
    Never stop learning!
    <--- please consider *-ing !

  3. #3
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Sorry, original post deleted as it contained sensitive info... Try this?
    Please Login or Register  to view this content.
    Tim
    Last edited by harrisonland; 04-19-2020 at 05:18 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Just an observation... It's not necessary to include "on error resume next", and it's really bad practice. The code above works without it.

  5. #5
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Hi Tim,

    Thank you for taking the time and helping me out, this is great stuff

    One thing, while this loop is running. For some reason certain emails in the inbox that it is cycling through cause the code to break at "Debug.Print Targetemail.SenderEmailAddress"
    saying Object doesn't support this property or method

    for example one email that was in my inbox was an undelivered mail from World-Gts-Postmaster telling me that the following email I sent was undelivered. There are certain cases that it breaks the code, is there a way to skip these items? You mentioned not to use On Error Resume Next.... Was hoping you can help me with this last little bit

    Please Login or Register  to view this content.
    Thanks Tim!

  6. #6
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187
    Hi Tim, I’m trying it now on a another PC and it works, I’m not sure if that was only my laptop that did that or because it was a weekend and didn’t have many emails. Either way looks good now thank you so much!

  7. #7
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Hmmm. That's strange. Wonder if it's a different version of Outlook, or Windoze or something.

    It's not a good idea to use "on error resume next", unless you anticipate a potential error and take steps to mitigate it. It can sometimes mask unforeseen errors, and you end up sitting there scratching your head wondering what's going on. I do sometimes use it if I don't know whether an object is going to exist, so I do something like
    Please Login or Register  to view this content.
    Here you could adopt a similar approach with
    Please Login or Register  to view this content.
    Notice how I've included "on error goto exitsub" twice - once in case the IF...THEN returns true, and the other in case not. The principle is you're minimising the amount of code that runs without error checking to avoid that unanticipated error that will otherwise be puzzling.

    Tim

  8. #8
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Interesting thank a lot Tim appreciate it this has been great help I wanted to thank you.

    I've been using this code the last week and its great, i am trying to do one adaptation to it now. Some emails cause a break, something to do with the senders address as it breaks here. "Debug.Print Targetemail.SenderEmailAddress"

    How can I error handlethat if this happens don't exit sub but go to next i. I think the error handle im adding is not working

    Also, another thing im trying to do is if it does not find my target Email Subject from Target Sender on Date = today, then to check Date -1 instead.... and loop -1 until it finds the email



    Please Login or Register  to view this content.
    If you knew how to do this quickly would appreciate it, ive been trying too.
    Last edited by lougs7; 04-28-2020 at 10:09 AM.

  9. #9
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Hi Tim, sorry to bother you was just wondering if you can pretty please help me with my last bit. My vba skills are not up to par im finding it difficult to do the last part, ive searched forums but no luck let me know

  10. #10
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Hi there. Sorry, been away for a few days. I wouldn't search "until a match is found" - otherwise you will end up with an infinite loop if there actually is not match.

    You could achieve recursing back a specified number of days by wrapping the actual search in another loop like this:
    Please Login or Register  to view this content.
    So the full code would be:
    Please Login or Register  to view this content.
    Tim
    Last edited by harrisonland; 05-02-2020 at 09:38 AM.

  11. #11
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Thank you so much. The probllem I am having now is that in the loop "For i = objFolder.Items.Count To 1 Step -1"

    objFolder.Items.Count is counting all my emails in the Inbox going back all the way to December so thousands of items, i = +10,000, where as I need it to count only the emails from today. So as it is now the Loop goes through all the i values, counting other days too, without Next DateToFind being triggered, so we never get a match.

    Im trying to restrict the Items.Count to count only Today's email.

    Also, I have some Undeliverable emails in the inbox, since these aren't Outllok "Mail Items", they are Outllok "Report Items" it is causing the code to break despite my error handles. Sorry I know I'm a pain
    Last edited by lougs7; 05-07-2020 at 02:12 PM.

  12. #12
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Might I suggest you get a grip on your inbox! 10,000 items!?!

    Seriously, though, the trouble is that, although new emails end up at the "top" of the email list, they aren't stored in strict date order (at least on my machine they aren't), so it's not as simple as quitting the loop once a certain date (e.g. today minus x-days) has been reached.

    One way around it would be to set up a filter in Outlook so your target emails end up in one folder and run the search on that. That would also fix the "Report Items" issue which don't have the elements you're looking for.

    In this bit of code Folders(1) is the mailbox (assuming you have only one account configured it'll be "1"). The Folders("Inbox") is then self explanatory.
    Please Login or Register  to view this content.
    So... if you create a Trade Notifications folder it would read
    Please Login or Register  to view this content.
    Or as a subfolder of the Inbox:
    Please Login or Register  to view this content.
    This would speed things up dramatically and you'd have the added benefit of an organised Inbox.

    Also, try changing targetemail.SentOn to targetemail.ReceivedTime... Report items might not have a "SentOn" time, but if the do have a "ReceivedTime" it might fix that issue. It's only a guess, because I don't have any reports to look at.
    Please Login or Register  to view this content.
    I'm not overly familiar with Outlook, so I'm not very sure on this one.


    Tim

  13. #13
    Forum Contributor
    Join Date
    09-23-2015
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    187
    Hi Tim. Wow this idea, which I’m not sure why I didn’t think of just saved me life. I made a rule in outlook created a sub folder. Set targetemail = objFolder.Items(1) and the whole necessity for a loop was removed..BINGO. I owe you a beer for your infinite support.

    Stay safe and have a good one. Cheer Tim! You hero

  14. #14
    Valued Forum Contributor
    Join Date
    06-22-2018
    Location
    Blackpool, England
    MS-Off Ver
    2019
    Posts
    408

    Re: Opening .Xls Attachement from Email based on Subject/Sender

    Well, sometimes it just takes a different pair of eyes to look at a problem - it's easy to get bogged down going down one rabbit hole when there are plenty of others to choose from.

    Glad you got there in the end!

    Tim

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 02-11-2019, 08:45 AM
  2. How to change email file from Subject to Sender's email address?
    By london7871 in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 09-25-2017, 11:13 PM
  3. Outlook Macro to save mail attachment from a specific sender with subject line
    By manoj.0790 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2016, 09:34 AM
  4. Forward Email Based on Selected Object's Subject and/or Sender and Recipient Address
    By Citanaf in forum Outlook Programming / VBA / Macros
    Replies: 2
    Last Post: 12-11-2015, 07:38 PM
  5. Save an Outlook attachment and move the email to a folder based and sender
    By db16886 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2014, 09:49 PM
  6. Opening emails based on subject
    By MikeFranz123 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 02:15 AM
  7. COPY outlook mails subject, received date, sender details in excel
    By Ratnakar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2012, 10:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1