Outlook Add-in: Remove hidden data and personal information in Word, Excel and PowerPoint attachments


A code sample for removing Word, Excel and/or PowerPoint hidden data and personal information in Outlook e-mail attachments:

private void button1_Click(object sender, RibbonControlEventArgs e)
{
 
    Microsoft.Office.Interop.Outlook.Inspector currentInspector =
        this.Context as Microsoft.Office.Interop.Outlook.Inspector;
 
    if (currentInspector == null)
    {
 
    }
 
    Microsoft.Office.Interop.Outlook.MailItem currentMailItem =
        currentInspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
    if (currentMailItem != null)
    {
        walkAttachments(currentMailItem);
 
    }
}
 
void FixWordDocument(string filePath)
{
    Word._Application wdApp = null; 
    Word._Document wdDoc = null;
    try
    {
        wdApp = new Word.Application();
        wdApp.Documents.Open(filePath);
        wdDoc = wdApp.ActiveDocument;
        Office.MsoDocInspectorStatus docStatus;
        string myResult;
        int intMetatDataType;
 
 
        for (intMetatDataType = 1; intMetatDataType <= 4; intMetatDataType++)
        {
            wdDoc.DocumentInspectors[intMetatDataType].Fix(out docStatus, out myResult);
        }
        wdDoc.RemoveDocumentInformation(Word.WdRemoveDocInfoType.wdRDIAll);
        wdDoc.Save();
        wdDoc.Close(Type.Missing, Type.Missing, Type.Missing);
        wdApp.Quit();
    }
    finally
    {
        Marshal.ReleaseComObject(wdDoc);
        Marshal.ReleaseComObject(wdApp);
        wdDoc = null;
        wdApp = null;
        GC.Collect(); 
    }
 
}
 
void FixExcelDocument(string filePath)
{
    Excel._Application xlApp = null;
    Excel._Workbook xlWkb = null;
    try
    {
        xlApp = new Excel.Application();
        xlApp.Workbooks.Open(filePath);
        xlWkb = xlApp.ActiveWorkbook;
        Office.MsoDocInspectorStatus docStatus;
        string myResult;
        int intMetatDataType;
 
        for (intMetatDataType = 1; intMetatDataType <= 4; intMetatDataType++)
        {
            xlWkb.DocumentInspectors[intMetatDataType].Fix(out docStatus, out myResult);
        }
        xlWkb.RemoveDocumentInformation(Excel.XlRemoveDocInfoType.xlRDIAll);
        xlWkb.Save();
        xlWkb.Close(Type.Missing, Type.Missing, Type.Missing);
        xlApp.Quit();
    }
    finally
    {
        Marshal.ReleaseComObject(xlWkb);
        Marshal.ReleaseComObject(xlApp);
        xlWkb = null;
        xlApp = null;
        GC.Collect(); 
    }
}
 
 
void FixPowerPointDocument(string filePath)
{
    PowerPoint._Application ppApp = null;
    PowerPoint._Presentation ppPres = null;
    try
    {
        ppApp = new PowerPoint.Application();
        ppApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized;
        ppApp.Presentations.Open(filePath);
        ppPres = ppApp.ActivePresentation;
        Office.MsoDocInspectorStatus docStatus;
        string myResult;
        int intMetatDataType;
 
        for (intMetatDataType = 1; intMetatDataType <= 4; intMetatDataType++)
        {
            ppPres.DocumentInspectors[intMetatDataType].Fix(out docStatus, out myResult);
        }
        ppPres.RemoveDocumentInformation(PowerPoint.PpRemoveDocInfoType.ppRDIAll);
        ppPres.Save();
        ppPres.Close();
        ppApp.Quit();
    }
    finally
    {
        Marshal.ReleaseComObject(ppPres);
        Marshal.ReleaseComObject(ppApp);
        ppPres = null;
        ppApp = null;
        GC.Collect();
    }
}
 
public void walkAttachments(Outlook.MailItem mailItem)
{
    if (mailItem.Attachments.Count > 0)
    {
        AttachmentCleanupDialog myForm = new AttachmentCleanupDialog();
 
        myForm.Show();
            myForm.progressBar1.Maximum = mailItem.Attachments.Count;
            myForm.closeButton.Enabled = false;
 
            List<string> aList = new List<string>();
            string temppath;
            string fileName;
            foreach (Outlook.Attachment oneAttachment in mailItem.Attachments)
            {
                myForm.progressBar1.Value += 1;
 
                if (oneAttachment.FileName.ToLower().Contains(".doc"))
                {
                    try
                    {
 
                        temppath = "C:\\Documents and Settings\\" + Environment.GetEnvironmentVariable("USERNAME").ToString() + "\\AppData\\Local\\Temp";
                        if (System.IO.Directory.Exists(temppath) == true)
                        {
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixWordDocument(temppath + "\\" + fileName);
                        }
                        else
                        {
                            System.IO.Directory.CreateDirectory(temppath);
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixWordDocument(temppath + "\\" + fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show("Attacment handler encountered the following exception: " + e);
                    }
                }
                if (oneAttachment.FileName.ToLower().Contains(".xls"))
                {
                    try
                    {
 
                        temppath = "C:\\Documents and Settings\\" + Environment.GetEnvironmentVariable("USERNAME").ToString() + "\\AppData\\Local\\Temp";
                        if (System.IO.Directory.Exists(temppath) == true)
                        {
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixExcelDocument(temppath + "\\" + fileName);
                        }
                        else
                        {
                            System.IO.Directory.CreateDirectory(temppath);
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixExcelDocument(temppath + "\\" + fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show("Attacment handler encountered the following exception: " + e);
                    }
                }
                if (oneAttachment.FileName.ToLower().Contains(".ppt"))
                {
                    try
                    {
 
                        temppath = "C:\\Documents and Settings\\" + Environment.GetEnvironmentVariable("USERNAME").ToString() + "\\AppData\\Local\\Temp";
                        if (System.IO.Directory.Exists(temppath) == true)
                        {
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixPowerPointDocument(temppath + "\\" + fileName);
                        }
                        else
                        {
                            System.IO.Directory.CreateDirectory(temppath);
                            fileName = oneAttachment.FileName;
                            oneAttachment.SaveAsFile(temppath + "\\" + fileName);
                            aList.Add(oneAttachment.FileName);
                            FixPowerPointDocument(temppath + "\\" + fileName);
                        }
                    }
                    catch (Exception e)
                    {
                        System.Windows.Forms.MessageBox.Show("Attacment handler encountered the following exception: " + e);
                    }
                }
            }
            myForm.progressBar1.Maximum = aList.Count;
            myForm.progressBar1.Value = 0;
            for (int i = 0; i < aList.Count; i++)
            {
                myForm.progressBar1.Value += 1;
                fileName = aList[i];
                foreach (Outlook.Attachment oneAttachment in mailItem.Attachments)
                {
 
                    temppath = "C:\\Documents and Settings\\" + Environment.GetEnvironmentVariable("USERNAME").ToString() + "\\AppData\\Local\\Temp";
                    if (oneAttachment.FileName == fileName)
                    {
                        oneAttachment.Delete();
                        mailItem.Attachments.Add(temppath + "\\" + fileName);
                        System.IO.File.Delete(temppath + "\\" + fileName);
                        break;
                    }
                }
 
            }
            aList.Clear();
            myForm.label1.Text = "Attachments successfully cleaned.";
            myForm.closeButton.Enabled = true;
        
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("No attachments found. Please attach a document first!");
    }
}

I am also attaching the Visual Studio solution. The add-in adds a button on the Message Ribbon tab for removing Office documents metadata.

AttachmentCleanup.zip

Comments (2)

  1. cron22 says:

    The question though is, since the concept of personal information, and that which is considered personal is different to different people, how would you psecify your preferences to outlook?  

  2. Andrei Ghita says:

    The types of hidden data and personal information stored in Office documents are described here: office.microsoft.com/…/remove-hidden-data-and-personal-information-from-office-documents-HA010037593.aspx

    The code above deletes all types of hidden data and personal information but one can choose the information to delete by choosing a specific type of information from the Word.WdRemoveDocInfoType enumeration: msdn.microsoft.com/…/bb243812(v=office.12).aspx

    For example, one could add a drop down list to the ribbon group allowing the user to choose the type of information to remove.

Skip to main content