VSTO storage control not saving the updates after the first save

One more interesting issue! Here is the story :

We had a VSTO customized document and we were trying to update the "cached data" in the doc but we found that it does not work after the first save (that is to say that for the first Save() everything is fine, but after that it does not get updated).

Below is the code that we tried:

 [Microsoft.VisualStudio.Tools.Applications.Runtime.Cached]
public String StrTest = "string";
public object oMissing = System.Reflection.Missing.Value;
private Word.Shape oRTS = null;
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
    System.Windows.Forms.MessageBox.Show(StrTest);

    StrTest = "string 1"; 
    Save(); '<<= Works fine

    StrTest = "string 2"; ' 
    Save(); '<<= Does not update the storage control with "string 2"

    StrTest = "string 3"; 
    Save(); '<<= Does not update the storage control with "string 3"

}

Given that this issue is bug, what can we do about it ? well.. the 'save' works only when the ActiveX control is 'dirty', but somehow our modifications are not dirtying it.

So, what can we do ? We need to do something with the storage control, but before even *doing* anything with storage control we need to get a reference for the control. Here is how we did it:

 

 private Word.Shape getRunTimeStorageControlShape()
{
Word.Shape oShape = null;
Word.Shapes oShapes = this.Shapes;
int iCntShapes = oShapes.Count;

for (int i = 1; i <= iCntShapes; i++)
{
object oIndex = i;
oShape = oShapes.get_Item(ref oIndex);
if (oShape.Type == 
Microsoft.Office.Core.MsoShapeType.msoOLEControlObject)
{
if 
(oShape.OLEFormat.ProgID.StartsWith("VSTO.RuntimeStorage"))
break;
}
}
return oShape;
}

 

Now, the Question is how to make the control dirty .. almost a no-brainer..change a property and revert it back to the original. Something like the following:

 

 private void markRuntimeStorageAsDirty()
{
if (null == oRTS) return;

oRTS.Width = oRTS.Width + 1;
oRTS.Width = oRTS.Width - 1;
}

 

And here is you modify the original code to plug in the changes:

 

 [Microsoft.VisualStudio.Tools.Applications.Runtime.Cached]
public String StrTest = "string";
public object oMissing = System.Reflection.Missing.Value;
private Word.Shape oRTS = null;
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
oRTS = getRunTimeStorageControlShape();
System.Windows.Forms.MessageBox.Show(StrTest);
StrTest = "string 1";
markRuntimeStorageAsDirty();
Save();
StrTest = "string 2";
markRuntimeStorageAsDirty();
Save();
StrTest = "string 3";
markRuntimeStorageAsDirty();
Save();
}

Howzzzat !? Did you like it ? Let me know ..

Okay ... bye ... take care !

 

del.icio.us tags: Pranav Wagh, Microsoft Blogger, VSTO, Troubleshooting, Code Samples

Technorati tags: Pranav Wagh, Microsoft Blogger, VSTO, Troubleshooting, Code Samples