none
XML Word AddIn and Content Controls

    Question

  • I want to load the template fields in a Word tempate doc with values from an xml file.  But I dont want to do this in the word document.cs. 

    Can I do this in the AddIn.cs? 

    I imagine it would just be something along the lines of : 
     Globals.ThisAddIn.Application.ActiveDocument.SomeControl.Datasource = xml file datareader

    thanks!
    • Edited by Renley Thursday, February 25, 2010 11:32 PM clarify
    Tuesday, February 23, 2010 10:28 PM

Answers

  • Ok, I found out what I was doing wrong.

    I was misplacing my code in regards to object creation.

    I have a Word PlugIn

    On the Ribbon I have a button (loadTempalteA)

    on the button click event for LoadTemplateA I load a specific template (.dotx)

    i then show a windows Form (modifyTemplateA)

    i then went to access the Tempalte ContentControls object array and got a null value

     IN correcting my code I placed the loading of TemplateA in the Form constructor after its InitializeCompoent method. 

    Now the Form has the object array of contentControls and can find any of the controls by Tag.


    • Marked as answer by Bessie Zhao Tuesday, March 02, 2010 2:07 AM
    Friday, February 26, 2010 6:29 PM

All replies

  • Perhaps a better question would be = how do i access the contentControls in the word template document?

    I want to accessa  specific contentControl named (for example) mytextPlaceholder

    do I have to search through the xml and get myTextPlaceholder's  xpath or can i just request the contentControl by name?

    Thursday, February 25, 2010 5:47 PM
  • Why does htis not return any controls when i know there are control in teh dotx file?

    I am executing this from a button in a windows Form.cs

                Word.ContentControls conCtrls;
                conCtrls = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTag("testTextHolder");
                Word.ContentControl conCtrl;
                conCtrl = conCtrls[0];
                label1.Text = conCtrl.PlaceholderText.Value;


    this works just fine from the form:
                detailTable = Globals.ThisAddIn.Application.ActiveDocument.Tables[1];
                detailTable.Cell(1, 3).Range.Text = "hello john";

    i clearly i have access to the document, since the hello john test gets added to the control in the table.

    so why does the first set of code not return any contentControls?

    Thursday, February 25, 2010 8:39 PM
  • This also states there are no controls:

    foreach (Word.ContentControl contentControl in Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTag("testTextHolder"))
                {
                    if (contentControl.Tag == "testTextHolder")
                    {
                        Globals.ThisAddIn.Application.Selection.InlineShapes.AddPicture(@"C:\Samplepic.jpg");
                    }
                }
    Thursday, February 25, 2010 8:40 PM
  • Hi Renley!

    Try doing this:

    - Select the content control that is present in your document
    - Click the Developer tab in the Office Ribbon
    - Click the Properties button in the Controls group
    - Check to see if the tag is equal to "testTextHolder"

    The only cause of the problem I can think of is that the content control you're trying to access has no tag.
    Just to clarify: the ID of the content control is NOT the same as the tag.

    Friday, February 26, 2010 2:50 AM
  • Thanks for the reply :)

    Whether it be in visual studio or just MS word, the button "Properties"  never activates, it just stays greyed out.  If i try to put a new control on the page or i select the control i already have on the page, even different types of controls, it the button never activates. 

    However in VS when I select my "testTextHolder" control and rightclick to go to properties i get the vs tab on the right that pops out and shows me the controls properties in VS:

    And in there the Tag property is set to "testTextHolder"  as well as the Text property as the Name.   The Control ID is 20468907  and there is no Title property set.  There are no databindings since I will be binding it to content from an XML file at runtime.   

    Additionally:
    I think this is supposed to return a group of all content controls

    Globals.ThisAddIn.Application.ActiveDocument.ContentControls

    But it returns null as if there are none. 

    Any ideas?


    p.s.
    If there is any code of mine i can post to help this process out feel free to let me know, cheers!

    Friday, February 26, 2010 4:14 PM
  • Ok, I found out what I was doing wrong.

    I was misplacing my code in regards to object creation.

    I have a Word PlugIn

    On the Ribbon I have a button (loadTempalteA)

    on the button click event for LoadTemplateA I load a specific template (.dotx)

    i then show a windows Form (modifyTemplateA)

    i then went to access the Tempalte ContentControls object array and got a null value

     IN correcting my code I placed the loading of TemplateA in the Form constructor after its InitializeCompoent method. 

    Now the Form has the object array of contentControls and can find any of the controls by Tag.


    • Marked as answer by Bessie Zhao Tuesday, March 02, 2010 2:07 AM
    Friday, February 26, 2010 6:29 PM