none
How to insert Word 2013 Start/End tag type Content Controls using OpenXML sdk RRS feed

Answers

  • Hi,

    According to your description, you want to insert a content control in a document and show as Start/End Tag with Open XML SDK.

    Here is a sample for your reference. You could use Open XML SDK Productivity tool to reflect code of an existing document with Start/End tag type Content Control. Then you can learn more about the Open XML elements from the code.

    I write a sample for your reference. To set the content control to show as Start/End Tag, we can set the properties in SdtProperties class such as SdtAlias, Tag and Appearance.

    static void InsertStartEndTagContentControl()
    {
        string strPath = @"C:\Documents\TestContentControl.docx";
        using (WordprocessingDocument objWordprocessingDocument =
                    WordprocessingDocument.Open(strPath, true))
        {
            /*
                * The Follwing code access the MainDocumentPart and 
                * Document node, Body node of the Word Document. These
                * existed already.
                */
            MainDocumentPart objMainDocumentPart =
                objWordprocessingDocument.MainDocumentPart;
            Document objDocument = objMainDocumentPart.Document;
            Body objBody = objDocument.Descendants<Body>()
                .FirstOrDefault();
            objBody.Append(GenerateSdtBlock());
            objDocument.Save();
        }
    
    }
    
    // Creates an SdtBlock instance and adds its children.
    public static SdtBlock GenerateSdtBlock()
    {
        SdtBlock sdtBlock1 = new SdtBlock();
    
        SdtProperties sdtProperties1 = new SdtProperties();
        SdtAlias sdtAlias1 = new SdtAlias() { Val = "Title" };
        Tag tag1 = new Tag() { Val = "Title" };
        W15.Appearance appearance1 = new W15.Appearance() { Val = W15.SdtAppearance.Tags };
    
        sdtProperties1.Append(sdtAlias1);
        sdtProperties1.Append(tag1);
        sdtProperties1.Append(appearance1);
    
        SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
    
        Paragraph paragraph1 = new Paragraph();
    
        Run run1 = new Run();
        Text text1 = new Text();
        text1.Text = "Test";
    
        run1.Append(text1);
    
        paragraph1.Append(run1);
    
        sdtContentBlock1.Append(paragraph1);
    
        sdtBlock1.Append(sdtProperties1);
        sdtBlock1.Append(sdtContentBlock1);
        return sdtBlock1;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 12, 2014 6:09 AM
    Moderator

All replies

  • Hello Antony,

    I'd suggest asking such questions in the Open XML Format SDK forum.

    Also you may find the Welcome to the Open XML SDK 2.5 for Office section in MSDN helpful.

    Saturday, August 9, 2014 7:43 AM
  • Hi Antony

    Which version of Word are you targeting / in which version of Word are you seeing the "tags" around the content control that you'd like to implement?

    In Word 2007 and 2010 you only see tags when the document is in Design Mode and I'm not sure that can be toggled.

    In Word 2013 there's also a content control display format that will display the tags, but these won't be recognized by any earlier versions of Word.

    I don't believe there's any way to store a setting in Word Open XML that will instruct Word to open the document in Design Mode, so the only possibility would be if you're targeting Word 2013 (and possibly later versions), only.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, August 9, 2014 5:09 PM
    Moderator
  • Thank You for your reply Cindy Meister, I am targeting Word 2013 only.


    Antony

    Sunday, August 10, 2014 5:32 PM
  • Hi,

    According to your description, you want to insert a content control in a document and show as Start/End Tag with Open XML SDK.

    Here is a sample for your reference. You could use Open XML SDK Productivity tool to reflect code of an existing document with Start/End tag type Content Control. Then you can learn more about the Open XML elements from the code.

    I write a sample for your reference. To set the content control to show as Start/End Tag, we can set the properties in SdtProperties class such as SdtAlias, Tag and Appearance.

    static void InsertStartEndTagContentControl()
    {
        string strPath = @"C:\Documents\TestContentControl.docx";
        using (WordprocessingDocument objWordprocessingDocument =
                    WordprocessingDocument.Open(strPath, true))
        {
            /*
                * The Follwing code access the MainDocumentPart and 
                * Document node, Body node of the Word Document. These
                * existed already.
                */
            MainDocumentPart objMainDocumentPart =
                objWordprocessingDocument.MainDocumentPart;
            Document objDocument = objMainDocumentPart.Document;
            Body objBody = objDocument.Descendants<Body>()
                .FirstOrDefault();
            objBody.Append(GenerateSdtBlock());
            objDocument.Save();
        }
    
    }
    
    // Creates an SdtBlock instance and adds its children.
    public static SdtBlock GenerateSdtBlock()
    {
        SdtBlock sdtBlock1 = new SdtBlock();
    
        SdtProperties sdtProperties1 = new SdtProperties();
        SdtAlias sdtAlias1 = new SdtAlias() { Val = "Title" };
        Tag tag1 = new Tag() { Val = "Title" };
        W15.Appearance appearance1 = new W15.Appearance() { Val = W15.SdtAppearance.Tags };
    
        sdtProperties1.Append(sdtAlias1);
        sdtProperties1.Append(tag1);
        sdtProperties1.Append(appearance1);
    
        SdtContentBlock sdtContentBlock1 = new SdtContentBlock();
    
        Paragraph paragraph1 = new Paragraph();
    
        Run run1 = new Run();
        Text text1 = new Text();
        text1.Text = "Test";
    
        run1.Append(text1);
    
        paragraph1.Append(run1);
    
        sdtContentBlock1.Append(paragraph1);
    
        sdtBlock1.Append(sdtProperties1);
        sdtBlock1.Append(sdtContentBlock1);
        return sdtBlock1;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 12, 2014 6:09 AM
    Moderator