ClaudiaHelpOnVSTO

Hi,

I have two different documents (A and B) and both have the same VSTO customization attached to them. [Actually I first create A and later open B and attach through the ServerDocument classe the VSTO solution].

How can I access a control instance (RichTextBox) of document A from B


I got as far as this:


// from document B, I try to find A
// iterate over the open documents

foreach (Word.Document parentDoc in Application.Documents)
{

    if(parentDoc.Name.Equals(Ħ°AĦħ)){
    // iterate of controls in document A
    System.Collections.IEnumerator en =
this.Controls.GetEnumerator();
    while (en.MoveNext())
    {

        MessageBox.Show(String.Format("Field: {0}",         en.Current.ToString()));

         }
    }
}

Thanks for any hint!

Claudia



Re: Visual Studio Tools for Office Access controls in different document instance (VSTO 2005)

John R. Durant - MSFT

Claudia,

As you have indicated, this can be done by gaining access to the other document in the OM and then walking the document objects. As you know, each doc has a Controls collection. You can access specific controls via the Item property. In VB.NET it looks like this (sorry, I had an existing sample vb.net I started with):

Dim docs As Word.Documents

Dim doc As Word.Document

docs = Me.Application.Documents

For Each doc In docs

If doc.Name = "WordDocument2.doc" Then

Dim t As Microsoft.Office.Tools.Word.Controls.TextBox

t = Me.Controls.Item(0)

t.Text = "hello world"

End If

Next

Best.






Re: Visual Studio Tools for Office Access controls in different document instance (VSTO 2005)

Meho

Hi,

I hope you have found a solution to you problem =)

anyway, I was wondering maybe if u could help me out since you mentioned that u have attached the VSTO customization to a document through ServerDocument.

this is the code i am using:

if(ServerDocument.IsCustomized(FilePath))

{

ServerDocument.RemoveCustomization(FilePath);

}

ServerDocument.AddCustomization(FilePath,..... etc);

this piece of code works perfectly when i deal with a VSTO word document (already attached to customization). However, when i use it for an ordinary word document, the call to AddCustomization fails with a COMException.

i appreciate your time & efforts

Maha





Re: Visual Studio Tools for Office Access controls in different document instance (VSTO 2005)

ClaudiaHelpOnVSTO

Hi Maha,
I am not an expert on this... but lets have a look what I have managed to do:

Most important first: I only managed to add the VSTO customization while the document was still closed!

1) create new Word instance
object notShow = false;
Word.Document newWordDoc = Globals.ThisDocument.Application.Documents.Add(ref missing, ref missing, ref missing, ref notShow);

2) save this newly created Word instance
object fileFormat = Word.WdSaveFormat.wdFormatDocument;
object fileName = @"C:/" + newWordDocName + ".doc";

newWordDoc.SaveAs(ref fileName, ref fileFormat, 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);

3) close the new Word instance
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
newWordDoc.Close(ref doNotSaveChanges, ref missing, ref missing);

4) add customization
AddNewCustomization("C:/" + newWordDocName + ".doc");

5) open up again the word document with the customization attached
notShow = true;
newWordDoc = Globals.ThisDocument.Application.Documents.Open(ref fileName, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref notShow,
ref missing, ref missing, ref missing, ref missing);


My AddNewCustomization method looks like this:
public void AddNewCustomization(string fileName)
{
try
{
if (!ServerDocument.IsCustomized(fileName))
{
string assemblyName = @"C:/deploy/jahia_temp_v1.0_1.0.0.116/jahia_temp_v1.0.dll";
string deploymentManifestPath =
@"C:/deploy/jahia_temp_v1.0_1.0.0.116/jahia_temp_v1.0.application";
string applicationVersion = "1.0.0.116";

string newManifest = ServerDocument.AddCustomization(
fileName, assemblyName, deploymentManifestPath,
applicationVersion, false);
}
else
{
MessageBox.Show("The specified document is already " +
"customized.");
}
}
catch (Exception ex)
{
ex.ToString();
}
}


I hope this helps!

Claudia




Re: Visual Studio Tools for Office Access controls in different document instance (VSTO 2005)

meho

Hey Claudia,

thanks for the post.. It was quite helpful =)

but I'm still getting this COMException with HResult 0x8040403 everytime i call AddCustomization() on a normal Word Document... any clue