none
Most stable and efficient method to Save Attachment to local folder RRS feed

  • Question

  • Hello everyone,

    I am not sure where else to turn for help with this.

    Can someone, PLEASE recommend a solution/Add-On I can install to automatically download specific attachment to a local folder? I need to be able to set a rule for the attachment using something like a wildcard: *.csv or *.docx.

    One of the solution I found is http://www.rsbr.de/Software/OASniffer/download.htm but it does not work.

    This is what I am currently doing and my issues:

    We use Outlook 2010 32bit on Exchange (Server).

    I created an inbox rule to take specific emails from my inbox, marked as read and place in a folder.
    Most importantly the inbox rule also run a script located in Outlook VB module.

    Script:

    Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim dateFormat
    dateFormat = Strings.Format(Now(), "mm_dd_yyyy_HH_MM_SS_AMPM")
    saveFolder = "C:\Users\Jarvis\Desktop\EmailAttachments"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & "My_Data_" & dateFormat & ".csv"
    Set objAtt = Nothing
    Next
    End Sub


    Everything works very well until I shout down my computer, or if Outlook crashes, OR the network/exchange server goes down. The script usually fails. The script will never work unless I do the following:

    1. Copy EVERYTHING out of your VB macro project, into a text file. Save the textfile to your drive.
    2. Close everything, reboot the computer.
    3. Go to your C:\Documents and settings\\Application Data\Microsoft\Outlook folder.
    4. Either delete the file VBAProject.otm, or (better and safer) rename to VBAProject.old or some similar name.
    5. Open Outlook
    6. Reopen the VB Macro editor
    7. Paste in the code from your text file.


    I have to do all of the above to get the script working again, this is required if I shut down the computer, restart, the network goes down or a crash.

    Is there an ADD-On I can install and use instead of this code? Is there any stable solution you can recommend that works even when outlook is not running or the computer restarts?


    Thank you so much.

    Synth.






    • Edited by Synthologic Saturday, December 15, 2012 9:19 AM
    • Moved by Max Meng Monday, December 17, 2012 3:06 AM (From:Outlook IT Pro Discussions)
    Saturday, December 15, 2012 8:03 AM

Answers

  • Hi Synthologic,

    I will create a Outlook add-in to handle your issue. In the add-in I will subscribe NewMailEx event to handle your issue. You can modify the code after my comment to fit your request:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Diagnostics;
    namespace CreateRule
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.NewMailEx += 
                    new Outlook.ApplicationEvents_11_NewMailExEventHandler(
                        Application_NewMailEx);            
            }
            void Application_NewMailEx(string EntryIDCollection)
            {
                Outlook.MailItem mail = Application.Session
                    .GetItemFromID(EntryIDCollection);
                if (mail != null && mail.Attachments.Count > 0)
                {
                    string path = @"C:\****************\";
                    foreach (Outlook.Attachment attachment in mail.Attachments)
                    {
                        try
                        {
                            //Please set your logic at this place
                            path += attachment.FileName;
                            Trace.TraceInformation(path);
                            attachment.SaveAsFile(path);
                            Trace.TraceInformation("Saved");
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError(ex.Message);
                        }
                    }
                }
            }
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
            #region VSTO generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    We cant create a rule which will execute VBA script by code. So subscribe NewMailEx event will make sense.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, December 18, 2012 2:25 AM
    Moderator

All replies

  • Hi Synth,

    Since it is coding related question so I have moved it to Outlook for Developers forum, community members there would be more experienced for such topics.

    Meanwhile, please check if this script would help:

    Save attachments from multiple selected items in Outlook (VBA)
    http://gallery.technet.microsoft.com/scriptcenter/Save-attachments-from-5b6bf54b


    Max Meng
    TechNet Community Support

    Monday, December 17, 2012 3:05 AM
  • Hi Synth.

    Thanks for posting in the MSDN Forum.

    I would recommend you create an add-in to handle your issue. Would you please tell me your Outlook version Visual Studio version for further research?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 17, 2012 8:09 AM
    Moderator
  • Hi Synth.

    Thanks for posting in the MSDN Forum.

    I would recommend you create an add-in to handle your issue. Would you please tell me your Outlook version Visual Studio version for further research?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Good morning,
    My Outlook is installed from: Office Professional Plus 2010. Version: 14.0.6123.5001 (32 bit)

    I have both Visual Studio 2010 and 2012 Ultimate and Professional.

    Thanks!

    Monday, December 17, 2012 4:13 PM
  • Hi Synthologic,

    I will create a Outlook add-in to handle your issue. In the add-in I will subscribe NewMailEx event to handle your issue. You can modify the code after my comment to fit your request:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using Office = Microsoft.Office.Core;
    using System.Diagnostics;
    namespace CreateRule
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.NewMailEx += 
                    new Outlook.ApplicationEvents_11_NewMailExEventHandler(
                        Application_NewMailEx);            
            }
            void Application_NewMailEx(string EntryIDCollection)
            {
                Outlook.MailItem mail = Application.Session
                    .GetItemFromID(EntryIDCollection);
                if (mail != null && mail.Attachments.Count > 0)
                {
                    string path = @"C:\****************\";
                    foreach (Outlook.Attachment attachment in mail.Attachments)
                    {
                        try
                        {
                            //Please set your logic at this place
                            path += attachment.FileName;
                            Trace.TraceInformation(path);
                            attachment.SaveAsFile(path);
                            Trace.TraceInformation("Saved");
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError(ex.Message);
                        }
                    }
                }
            }
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
            #region VSTO generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    We cant create a rule which will execute VBA script by code. So subscribe NewMailEx event will make sense.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, December 18, 2012 2:25 AM
    Moderator