Hi all,

I am using VSTO 2005 SE with Office 2003 (Office 11). I have a Word template document with an customized VSTO assembly that my C# program copies when a user attempts to open the document via my program. The copied document correctly maintains the assembly information and opens successfully in Word.

The copied document is password protected to prevent editing. Using bookmarks, the code-behind enables editing in certain areas of the document where the user will fill in information. Standard stuff.

The problem is that when the user saves the document after making changes in the editable area, the assembly information is dropped from their copy of the document unless I remove the password protection from the document before saving. If I programmatically turn off the password protection just before the save, the custom assembly will remain attached to the document file as I would expect, but my document is left unprotected after the save. I added the "unprotect" code in the BeforeSave event, as CTRL+S and AutoSave bypass the virtual Save() function. By doing that though, there is no AfterSave event where I can turn the document password protection back on after the save.

Is this a known bug that VSTO assemblies become detached when CTRL+S, Autosave, or even the FILE|SAVE menu choice save the document when the document is password protected Or am I missing something obvious Is there a way to keep my password protection on the document during save, and keep the customization attached to my document

thanks for any pointers!


Re: Visual Studio Tools for Office VSTO customization detachs on save when document is password protected

Ji Zhou ¨C MSFT


I think as a workaround, you can use a Timer to protect the document again after saving the document.

And how do you copy the document and protect it Could you please post some codes That will be helpful to reproduce the issue.



Re: Visual Studio Tools for Office VSTO customization detachs on save when document is password protected


Sorry for the long delay in replying...

Timers are not an option, as saving the document can take anywhere from 1 second to as much as 30 seconds, depending on where the user is accessing the document from (VPN, remote office, etc). If there were only an AfterSave() event.

The original template document is copied using System.IO.File.Copy(). The original .DOC file is in the same directory as where the copy is created.

The code is pretty simple for the save:

public void SaveDocument()


bool bWasProtected = IsProtected;



this.Unprotect(ref m_oKey);









// If the doc was protected when we tried to save, re-protect it

if (bWasProtected)


object noReset = true;

object useIRM = System.Type.Missing;

object enforceStyleLock = false;


ref noReset, ref m_oKey, ref useIRM, ref enforceStyleLock);


// Changing the protection makes Word think that the doc has changed forcing

// this.Saved back to true. Since we just saved it successfully, we know that no

// content has changed so we force Word to think the doc is saved

this.Saved = true;



Commenting out the "this.Unprotect()" and "this.Protect()" lines allows the save to still occur, but when the document is next opened, an error pops up saying "The customization assembly could not be found or could not be loaded. You can still edit and save the document. Contact your administrator of the author of this document for further assistance."

There is a DETAILS button, and an OK button. Clicking DETAILS in this situation expands the popup to show an empty text box. There are no details. If I search the contents of the .DOC file there is no longer a reference to the path/assembly name embedded in the document file.

One thing to note. This project was originally written using VSTO 2003 using Visual Studio 2003, then converted to VS2005 and VSTO 2005 SE. The assembly information for using VSTO 2005 was assigned using the ApplicationManifestEditor sample app. A relative file path is used for referencing the assembly, with the assembly being located in a sub-directory one level below the document directory.

The problem I originally wrote about is compounded by the fact that CTRL-S and AutoRecovery saves bypass my event handler that calls my SaveDocument() function. The Unprotect() and Protect() code doesn't execute in these two cases, causing the assembly to be become detached. If I try to trap for this in the DocumentBeforeSave event handler, setting Cancel = true on any save other than the clicking of my custom SAVE button, then AutoRecovery pops up a message saying AutoRecovery save has been delayed for [DocumentName] which is very confusing to the end user.

Thanks for any further suggestions