locked
Outlook Macro to open attachments RRS feed

  • Question

  • I get about 50-60 automated emails from our point of sale system (sales statistics, etc) once a week and at every month end, it's about 100 or so.  

     I need a macro to do the following:

    - go to [Outlook] Inbox --> [sub folder] Store Statistics

    From the Store Statistics folder, check for any unread emails that have an attachment (the attachment will always end in *.txt.gpg or *.htm.gpg)

    Open the email and the attachment, then close the attachment and the email and move to the next unread email in the folder (no subfolders are to be checked).  

    has anyone done anything like this or are there examples to get me started?

    KSJ

    Wednesday, January 25, 2012 6:48 PM

Answers

All replies

  • You can't directly open an attachment, you have to save it to the file system and open the file from there. Outlook does that under the hood, it saves the attachment to a temp folder and then spawns the application registered for the Open verb for that file extension.
     
    What would open a .gpg file?
     
    Something like this snippet would do the basics of finding every unread item.
     
    Sub DoWhatever()
        Dim f As Outlook.Folder
        Set f = Application.Session.GetDefailtFolder(olFolderInbox).Folders.Item("Store Statistics")
        Dim cItems As Outlook.Items
        Set cItems = f.Items
        Dim oMail As Outlook.MailItem
        Dim o As Object
        For Each o In cItems
            If o.Class = olMail Then
                Set oMail = o
                If oMail.Unread Then
                    oMail.Display 'now get Attachments collection, iterate it and save each attachment somewhere
                End If
            End If
        Next
    End Sub

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Kala Sha Jahan" <=?utf-8?B?S2FsYSBTaGEgSmFoYW4=?=> wrote in message news:1762cc73-0bb3-4216-a777-cf722bc39e09...

    I get about 50-60 automated emails from our point of sale system (sales statistics, etc) once a week and at every month end, it's about 100 or so.  

     I need a macro to do the following:

    - go to [Outlook] Inbox --> [sub folder] Store Statistics

    From the Store Statistics folder, check for any unread emails that have an attachment (the attachment will always end in *.txt.gpg or *.htm.gpg)

    Open the email and the attachment, then close the attachment and the email and move to the next unread email in the folder (no subfolders are to be checked).  

    has anyone done anything like this or are there examples to get me started?

    KSJ


    Ken Slovak MVP - Outlook
    Wednesday, January 25, 2012 9:52 PM
  • Simply span process to open your file after you save attachment to disk, something like this:

    var psi = new ProcessStartInfo(file.FullFileName) {Verb = "Open"};
    Process p = Process.Start(psi);
    

    you can even attach to Exit event on process to know when user finished inspection of file, be aware though, that when file is opened by windows explorer, i.e. picture, you will not get Exit event called even if user closes window 

     

    as a side note: On Outlook 2010 you can access contents of attachment without saving it to disk, as described in http://msdn.microsoft.com/en-us/library/ee692174.aspx.

    Thursday, January 26, 2012 8:47 AM
  • Kala I can recommend you macro redy to import: Export specyf. Attach from User and dates

    I publish on Technet Wiki

    Interfejs programu

    Translate article by google or anothet to your lang.


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved


    • Edited by VBAToolsMVP Thursday, January 26, 2012 11:59 AM
    • Proposed as answer by 许阳(无锡) Friday, January 27, 2012 4:54 AM
    • Marked as answer by 许阳(无锡) Thursday, February 9, 2012 8:16 AM
    Thursday, January 26, 2012 11:55 AM