none
How to properly edit/modify a received or sent mail Item body that is protected using word interop

    Question

  • I want to edit the mail item’s body of a sent item in order to hide/remove in my case some sensitive information. I’m using word bookmarks in my template to flag those range and I want after the email have been sent to remove those contents.

    The issue seems that once sent, the mail body is protected. Using the “unprotect” Method on the word document I’m able to edit my email but then I’m unable to save it using the save method of the mail item or of the doc itself.

    I was wondering what was the proper way of editing the body of a sent email.

    Thanks in advance guys. Here is the code.

    			try
    			{
    				Outlook.Inspector inspector = oMail.GetInspector;
    
    				if (inspector == null) return;
    
    				if (inspector.IsWordMail())
    				{
    					Word.Document doc = inspector.WordEditor as Word.Document;
    					Word.WdProtectionType pType = doc.ProtectionType;
    					if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection) doc.Unprotect();
    
    					foreach (Word.Bookmark mark in doc.Bookmarks)
    					{
    						if(mark.Name.StartsWith("SensitiveInformation"))
    						{
    							try
    							{
    								String tmp = new String('*', mark.Range.Text.Length);
    								mark.Range.Text = tmp;
    							}
    							catch (Exception e)
    							{
    								Util.OnError(e);
    							}
    						}
    					}
    
    					if (doc.ProtectionType == Word.WdProtectionType.wdNoProtection)
    					{
    						// Not Allowed!! Hum...
    						//doc.Protect(Word.WdProtectionType.wdAllowOnlyReading);
    						
    					}
    					if (!oMail.Saved) oMail.Save();
    				}
    				
    			}
    			catch (Exception e)
    			{
    				Util.OnError(e);
    			}

    Monday, January 14, 2013 1:48 PM

Answers

  • Outlook.Inspector inspector = oMail.GetInspector;
    
    if (inspector == null) return;
    oMail.Display(false);
    
    if (inspector.IsWordMail())
    {
    	Word.Document doc = inspector.WordEditor as Word.Document;
    	if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection) doc.Unprotect();
    
    	foreach (Word.Bookmark mark in doc.Bookmarks)
    	{
    		if(mark.Name.StartsWith("SensitiveInformation"))
    		{
    			try
    			{
    				String mask = new String('*', mark.Range.Text.Length);
    				mark.Range.Duplicate.Text = mask;
    			}
    			catch (Exception e)
    			{
    				Util.OnError(e);
    			}
    		}
    	}
    
    	if (!oMail.Subject.EndsWith("(Masked)")) oMail.Subject = oMail.Subject + " (Masked)";
    	oMail.Save();
    
    	((Outlook._MailItem)oMail).Close(Outlook.OlInspectorClose.olSave);
    
    }

    This last code seems to work fine. Note the MailItem.Display() ? It seems that you cannot modify and save a mailitem using the word object model without showing it in the inspector. From what I suspect this is probably due to the fact that MailItem.Save() Method only save the mail properties and that ((Outlook._MailItem)oMail).Close(Outlook.OlInspectorClose.olSave) is the one that is actually saving the (word) body. And of course there must be a nice check at the beginning that is checking if the inspector is displayed, so if you want to enter this piece of code you need to show it first...

    This is a major drawback that make look the automation like **** (fast windows opening and closing).  From what I’ve gathered around there don’t seems to be any alternative… If you know of any way, please feel free to share, and if any MS representative ever read this, come on guys you should really consider a way to change this for future release.



    • Marked as answer by Herve Roux Tuesday, January 15, 2013 1:52 PM
    • Edited by Herve Roux Tuesday, January 15, 2013 1:53 PM
    Tuesday, January 15, 2013 1:52 PM

All replies

  • If you are not using the Word object model, you should be able to reset the body using the HTMLBody or Body properties.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, January 14, 2013 1:55 PM
  • Do you mean that this is not possible using the Word Object model?

    As a dirty workaround this seems to work...

    String mask = new String('*', mark.Range.Text.Length);
    //mark.Range.Text = tmp;
    if (HTMLBody == null) HTMLBody = oMail.HTMLBody;
    HTMLBody = HTMLBody.Replace(System.Web.HttpUtility.HtmlEncode(mark.Range.Text), mask);
    
    ...
    
    if (HTMLBody != null) oMail.HTMLBody = HTMLBody;
    
    But this is not even close to "properly" edit!! It's amazing that there is nothing out there speaking of this issue!
     
    • Edited by Herve Roux Monday, January 14, 2013 3:56 PM
    Monday, January 14, 2013 2:13 PM
  • So what exactly do you mean by "the mail body is protected"?

    Do you get an error when you call MailItem.Save? What is it?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, January 14, 2013 4:19 PM
  • String mask = new String('*', mark.Range.Text.Length);
    mark.Range.Text = mask;

    If you try this code you get the following exception.

    "You are not allowed to edit this selection because it is protected."

    If you check the "doc.ProtectionType" it is set to "Word.WdProtectionType.wdAllowOnlyReading"

    after doing a doc.Unprotect(); the doc.ProtectionType is set to Word.WdProtectionType.wdNoProtection

    then the mark.Range.Text = mask; is working fine. (we are in the end simulating the inspector "action > Edit Message" of the read only mode.)

    But what is the issue in the end is that I'm unable to save the body. I say body, and I think we need to find a way to save it at the word level, because the fun things is that after that if I go to the email, double click to open the "read only" mode inspector, the body is in editable state. And when exiting it's asking "The body of the message <subject> has been changed. Do you want to save changes to this message?" If I select Yes the body is then saved and my changes are committed. The point is how I can commit them from the object Model???

    Anyway thanks a lot Dmitry for your interest. I really appreciate it!

    • Edited by Herve Roux Tuesday, January 15, 2013 10:31 AM
    Tuesday, January 15, 2013 10:26 AM
  • Outlook.Inspector inspector = oMail.GetInspector;
    
    if (inspector == null) return;
    oMail.Display(false);
    
    if (inspector.IsWordMail())
    {
    	Word.Document doc = inspector.WordEditor as Word.Document;
    	if (doc.ProtectionType != Word.WdProtectionType.wdNoProtection) doc.Unprotect();
    
    	foreach (Word.Bookmark mark in doc.Bookmarks)
    	{
    		if(mark.Name.StartsWith("SensitiveInformation"))
    		{
    			try
    			{
    				String mask = new String('*', mark.Range.Text.Length);
    				mark.Range.Duplicate.Text = mask;
    			}
    			catch (Exception e)
    			{
    				Util.OnError(e);
    			}
    		}
    	}
    
    	if (!oMail.Subject.EndsWith("(Masked)")) oMail.Subject = oMail.Subject + " (Masked)";
    	oMail.Save();
    
    	((Outlook._MailItem)oMail).Close(Outlook.OlInspectorClose.olSave);
    
    }

    This last code seems to work fine. Note the MailItem.Display() ? It seems that you cannot modify and save a mailitem using the word object model without showing it in the inspector. From what I suspect this is probably due to the fact that MailItem.Save() Method only save the mail properties and that ((Outlook._MailItem)oMail).Close(Outlook.OlInspectorClose.olSave) is the one that is actually saving the (word) body. And of course there must be a nice check at the beginning that is checking if the inspector is displayed, so if you want to enter this piece of code you need to show it first...

    This is a major drawback that make look the automation like **** (fast windows opening and closing).  From what I’ve gathered around there don’t seems to be any alternative… If you know of any way, please feel free to share, and if any MS representative ever read this, come on guys you should really consider a way to change this for future release.



    • Marked as answer by Herve Roux Tuesday, January 15, 2013 1:52 PM
    • Edited by Herve Roux Tuesday, January 15, 2013 1:53 PM
    Tuesday, January 15, 2013 1:52 PM
  • i think that both PR_RTF_COMPRESSED and PR_BODY_HTML contain elements that correspond to your bookmarks, you can use those properties to find parts of document that you can change.
    Tuesday, January 15, 2013 7:03 PM