Why do I have multiple instances of TrinStgClass when cut-n-pasting VSTO docs?

I have a number of documents that have been created using a VSTO 2005 template. If I composite these documents together using either cut-n-paste or InsertFile, then save, close and reopen the composited document, I get a "file has macros" warning where no macros existed in any of the source documents. If I open the document and VBA editor, I can see no macros - however I do see multiple instances of the TrinStgClass ((VSTO.RuntimeStorage class) in the project - (TrinStgClass1, TrinStgClass2, TrinStgClass4, TrinStgClass8, TrinStgClass12, etc.). I have found that if I save the document as XML, then re-save it back to the .doc format, the "spurious" macro warning disappears, however I am still left with the multiple TrinStgClass objects (which I cannot delete - they are "in use").

It was my understanding that VSTO can deal with this scenario and should consolidate multiple storage instances upon save - any idea as to why this is failing to occur The macro warning is particularily troubling to my users as they are concerned they are subject to a virus - any thoughts

I have tried this with a number of scenarios and received the same results:

  • Removed the VSTO assembly from the source documents prior to combining them (used ServerDocument - unfortunately, this does not remove the storage components from the document).
  • Created the combined document from a VSTO template.
  • Created the combined document from Normal.dot (i.e. no VSTO customization)



Answer this question

Why do I have multiple instances of TrinStgClass when cut-n-pasting VSTO docs?

  • James Ogden

    If you had a VSTO customization attached to the "consolidated" Word document:
    =============================================================

    The VSTO runtime performs a sweep and deletes all spurious Runtime Storage Controls (RSC) in a document on the Before Save event. So, the first time you open a VSTO-customized doc with spurious RSCs you will still see the macro security dialog.

    Unfortunately, we just found a bug whereby only half of the spurious RSCs in a document get deleted each time the document is saved e.g. to delete all 23 spurious RSCs you would have to trigger the BeforeSave event at least 4 times.

    We are investigating this bug.

    If you do NOT have a VSTO customization attached to the "consolidated" doc:
    ============================================================

    Your consolidated document is a plain vanilla doc. So, VSTO is not even running when the doc is open. And as far as Word is concerned, the RSC is just another ActiveX control in the document. So, in this case you would have to find and delete all the RSC yourself.

    How to delete RSCs yourself
    =======================
    You can do this using Word automation by looping thru all the Shape objects in the Document and deleting the ActiveX controls that are RSCs. Although I have used managed code, you may use script or C++.

    Word.Document doc = app.Documents.Open(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing);
    Word.
    Shapes shapes = doc.Shapes;

    int delCtr = 0;
    for (int i = 1; i + delCtr <= shapes.Count; i++)
    {
       
    // Casting to IRuntimeStorage causes .NET to perform a QI for the interface
       
    // This will succeed only on an ActiveX control that implements IRuntimeStorage
       
    try
       
    {
           
    object i_obj = i;
            VSTOStgLib.
    IRuntimeStorage rsc = (VSTOStgLib.IRuntimeStorage)shapes.get_Item(ref i_obj).OLEFormat.Object;

            // If cast succeeds we have a RSC. Delete it.
           
    shapes.get_Item(ref i_obj).Delete();
           
    delCtr++;
            
    i--;
        }
       
    catch (InvalidCastException) {
           
    // Do nothing. This is not a RSC.
        }
    }
    doc.Save();
    doc.Close(
    ref missing, ref missing, ref missing);

    VSTOStgLib in the namespace created if you add a reference to the VSTORuntimeStorageCtrl.dll in your VS project. This dll is usually found in Program Files\Common Files\Microsoft Shared\VSTO\8.0 folder.

    It seems that deleting all spurious RSCs stops the macro secutiry dialog. Two possible solutions:
    1.
    -
    Make temporary copies of each "sub-doc".
    - Remove VSTO customization from the temp copy (you can use VSTO ServerDocument object)
    - Scrub the temp copy of all RSCs using Word automation (code above)
    - Then perform your insert into the master doc

    2.
    - Perform all the inserts into the master doc. Save it.
    - Scrub the doc of all Run Stg controls using Word automation (code above)

    Please note that there may be ways of copying just the text from Word docs and excluding controls etc. This would make your like easier, of course, assuming you do not want to copy controls.



  • CsOtto

    That worked perfectly - thank you.

  • sahil1286

    Thanks for the response.

    I am not actually attempting to do anything directly with the storage containers - the issues I describe are unfortunate side-effects of merging multiple VSTO 2005 documents into a single document. To further clarify, take the following scenario:

    1. I have two documents - Doc1.doc and Doc2.doc
    2. Both of these documents were created from my VSTO 2005 template, MyVstoTemplate.dot.
    3. I need to produce a 3rd document (Doc3.doc) from the contents of documents Doc1.doc and Doc2.doc.
    4. I open Doc1.doc (no macro warning is displayed) and SaveAs Doc3.doc.
    5. I open Doc2.doc (no macro warning is displayed), select all and copy to the clipboard.
    6. I return to Doc3.doc, move to the end of the document, and insert the contents of the clipboard.
    7. I close Doc2.doc (without saving - no changes have been made)
    8. I save and close Doc3.doc
    9. I re-open Doc3.doc - I immediately get a macro security warning stating there are macros in the document (where previously there were none). Opening the VBA editor, there are no macros visible, however I see that there are two instances of the TrinStgClass object attached to the document.

    Does that help



  • Stephen Graham

    Thank you for your detailed response - I shall implement your suggested fix immediately and post the results.

  • MTarek

    Hi Darren

    I am not quite sure what you are trying to achieve with regards to the Storage container you may have to have an application that gathers the ServerDocument information and merge this information yourself, I have never seen anything saying a copy/paste from multiple documents within Word would copy the Storage containers into one as the master document would in my opinion would maintain the correct item. Your example of the trinStgClassx is likely to be the cause of this,

    Sorry if I dont understand as to what you are trying to achieve may help me describe what to do.

    Regards



  • Why do I have multiple instances of TrinStgClass when cut-n-pasting VSTO docs?