none
VSTO - Outlook 2016 - Exclude files or folder from attachments MRU RRS feed

  • Question

  • Hi, 

    I'm working on a Outlook addin that insert files in the body of the mail.

    I'm using Range.InsertFile method for the insertion.

    But i noticed that all files inserted are listes in the attachment recent files.

    (https://blogs.office.com/2015/11/09/attachments-in-outlook-2016-ready-for-collaboration/)

    I didn't found a way to exclude theses files.

    I tried to exclude them from the Window's MRU but it is not repercuted in Outlook.

    If someone have an information.

    Regards.
    Monday, December 5, 2016 8:54 AM

Answers

  • Hi,

    I find the solution here.

    We need to remove the shortcut in the following folder, then the file would be removed from the list.

    C:\Users\%username%\AppData\Roaming\Microsoft\Office\Recent

    C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent

    Please see the code below.

    In ThisAddIn.cs

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.Inspectors.NewInspector += Inspectors_NewInspector;
            }
      private void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                Object item = Inspector.CurrentItem;
                if (item is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = item as Outlook.MailItem;
                    mailItem.AttachmentAdd += MailItem_AttachmentAdd;
                }
            }
         public string file1;
            public string file2;
            public Boolean flag;
            private void MailItem_AttachmentAdd(Outlook.Attachment Attachment)
            {         
                string fileName = Attachment.FileName +".lnk";
                string Path1 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Office\Recent";
                string Path2 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+@"\Microsoft\Windows\Recent";
                file1 = System.IO.Path.Combine(Path1, fileName);
                file2 = System.IO.Path.Combine(Path2, fileName);
                if (flag == false)
                {
                    if (System.IO.File.Exists(file1))
                    {
                        System.IO.File.Delete(file1);
                    }
                    if (System.IO.File.Exists(file2))
                    {
                        System.IO.File.Delete(file2);
                    }
                }        
            }
    

    In Ribbon.cs

    private void InsertAttach_Click(object sender, RibbonControlEventArgs e)
            {
                Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                Microsoft.Office.Interop.Outlook.Inspector myInspector = mailItem.GetInspector;
                Word.Document wdDoc = myInspector.WordEditor;
                Word.Range wdRng = wdDoc.Application.Selection.Range;
                wdRng.InsertFile(@"AttachmentFilePath");
                Globals.ThisAddIn.flag = false;
            }
    

    If it does not work for you, please let me know.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 28, 2016 8:56 AM
    Moderator

All replies

  • Hi,

    Office object model doesn’t provide any method or properties for Recent Items List.

    But you could try to remove all files in C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent to remove local files.

    Or you could disable it by setting MaxAttachmentMenuItems Registry value

    Key: HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Options\Mail

    Value name: MaxAttachmentMenuItems

    Value type: REG_DWORD

    Value: 0

    I think the work arounds above might not meet your requirement, I would suggest you submit a feedback on the UserVoice: https://outlook.uservoice.com/

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 6, 2016 4:18 AM
    Moderator
  • Hi, thanks for your reply.

    You're right, deleting files in Recent directory has no repercution in recent attachments (Outlouk).

    I also tried to create my own file extension and to exclude it from Window's recent. 

    I already saw MaxAttachmentMenuItems but I don't want to disable the feature.

    If there is no possibility I'll try the UserVoice, but it's a little bit hopeless.

    Regards

    Tuesday, December 6, 2016 8:25 AM
  • Hi,

    If you still want to achieve that, we may create a custom list like what Outlook provides now. We could create a combobox and handle BeforeAttachmentAdd Event or AttachmentAdd Event  and then store attachments in a temp folder, get items from the temp folder. When handling the event, we could check how the attachment is added, add some flags to the attachment when adding from VSTO add-ins, then save it into the temp folder If it is not added by the VSTO add-ins.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 16, 2016 4:39 AM
    Moderator
  • Hi Celeste !

    I am not really sure to understand what you want to do, but I already tried to handle all events concerning attachments, but none is fired when inserting my file in the body.

    If you think there exists some flags to be added to the file before the insertion to hide it from recent list, we can continue in this way.

    Regards

    Friday, December 16, 2016 9:09 AM
  • Hi,

    I find the solution here.

    We need to remove the shortcut in the following folder, then the file would be removed from the list.

    C:\Users\%username%\AppData\Roaming\Microsoft\Office\Recent

    C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Recent

    Please see the code below.

    In ThisAddIn.cs

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                Application.Inspectors.NewInspector += Inspectors_NewInspector;
            }
      private void Inspectors_NewInspector(Outlook.Inspector Inspector)
            {
                Object item = Inspector.CurrentItem;
                if (item is Outlook.MailItem)
                {
                    Outlook.MailItem mailItem = item as Outlook.MailItem;
                    mailItem.AttachmentAdd += MailItem_AttachmentAdd;
                }
            }
         public string file1;
            public string file2;
            public Boolean flag;
            private void MailItem_AttachmentAdd(Outlook.Attachment Attachment)
            {         
                string fileName = Attachment.FileName +".lnk";
                string Path1 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Office\Recent";
                string Path2 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+@"\Microsoft\Windows\Recent";
                file1 = System.IO.Path.Combine(Path1, fileName);
                file2 = System.IO.Path.Combine(Path2, fileName);
                if (flag == false)
                {
                    if (System.IO.File.Exists(file1))
                    {
                        System.IO.File.Delete(file1);
                    }
                    if (System.IO.File.Exists(file2))
                    {
                        System.IO.File.Delete(file2);
                    }
                }        
            }
    

    In Ribbon.cs

    private void InsertAttach_Click(object sender, RibbonControlEventArgs e)
            {
                Outlook.MailItem mailItem = Globals.ThisAddIn.Application.ActiveInspector().CurrentItem;
                Microsoft.Office.Interop.Outlook.Inspector myInspector = mailItem.GetInspector;
                Word.Document wdDoc = myInspector.WordEditor;
                Word.Range wdRng = wdDoc.Application.Selection.Range;
                wdRng.InsertFile(@"AttachmentFilePath");
                Globals.ThisAddIn.flag = false;
            }
    

    If it does not work for you, please let me know.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 28, 2016 8:56 AM
    Moderator
  • Hi, you are right : removing shortcut from both directories seems to do the job.
    Thank you very much.
    Best Regards.

    string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string fileName = $"{Path.GetFileName(filePath)}.lnk";
    
    string officeRecent = $@"{roaming}\Microsoft\Office\Recent\{fileName}";
    string windowsRecent = $@"{roaming}\Microsoft\Windows\Recent\{fileName}";
    
    if (File.Exists(officeRecent)) File.Delete(officeRecent);
    if (File.Exists(windowsRecent)) File.Delete(windowsRecent);

    Wednesday, December 28, 2016 2:00 PM