none
Creating Unordered Lists in Content Control using InsertXML() Help needed RRS feed

  • Question

  • Hello all,

    I am using Word 2007 and VS2008 and I am trying to do the following.  Writing an unordered list within a content control programatically using the InsertXML method.

    The code I have is the following:

    object missing = System.Type.Missing;
    string ccXML = string.Empty;
    
    ccXML = "<?xml version='1.0' standalone='yes'?><?mso-application progid='Word.Document'?><w:wordDocument xmlns:aml='http://schemas.microsoft.com/aml/2001/core' xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882' xmlns:ve='http://schemas.openxmlformats.org/markup-compatibility/2006' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w10='urn:schemas-microsoft-com:office:word' xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' xmlns:wsp='http://schemas.microsoft.com/office/word/2003/wordml/sp2' xmlns:sl='http://schemas.microsoft.com/schemaLibrary/2003/core' w:macrosPresent='no' w:embeddedObjPresent='no' w:ocxPresent='no' xml:space='preserve'>";
    
    ccXML = ccXML + "<w:body><w:p><w:pPr><w:pStyle w:val='ListParagraph'/><w:numPr><w:ilvl w:val='0'/><w:numId w:val='1'/></w:numPr></w:pPr><w:r><w:t>This is purely a test.</w:t></w:r></w:p></w:body></w:wordDocument>";
    
    ptccStuff.Range.InsertXML(ccXML, ref missing); //Our content control (Plain Text Content Control)
    rtccStuff.Range.InsertXML(ccXML, ref missing); //Our content control (Rich Text Content Control)
    
    
    

    The text that should be bulleted is shown but no bullet points at all.

    • Moved by Cindy Meister MVPModerator Thursday, May 19, 2011 12:59 PM OpenXML syntax required (From:Visual Studio Tools for Office)
    Wednesday, May 11, 2011 5:16 PM

Answers

  • AFAICS the following will insert a couple of bulleted paras. it's in VBA so you'll need to translate back. As you will see, the key is that you have to "connect" the paragraphs to a listDef via an ilfo. However, without a lot more research it would be difficult to determine how reliable the insertion technique is, because when you are inserting into Word (rather than into an XML document) Word has to merge the XML you are inserting with what's there. I think it will probably create a new listDef and renumber the listDefs, but I'm not sure. So it's probably better to insert using the Object Model instead of XML if you can.

    AFAICS a lot of work has been done on numbering and bullets in the last couple of versions of Word -

     a. Word itself now seems to favour MultiLevel lists. I'm not sure it ever creates a SingleLevel list like this any more.

     b. there is doubtless stuff that you can do in Word 2007/2010 that canot be represented in the Word 2003 ML we're using here.

     c. there seems to be quite a lot of flexibility. e.g., here I do not specify a paragraph style. But you can, and you can connect that to the Listdef instead.

     

    Sub insertSomeBullets()

    ' inserts a couple of bulleted paras into the Selection

    ' select the CC's text, and run it.

    Dim ccXML As String

    ccXML = "<?xml version='1.0' standalone='yes'?>"

    ccXML = "<w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' w:macrosPresent='no' w:embeddedObjPresent='no' w:ocxPresent='no' xml:space='preserve'>"

    ccXML = ccXML + "<w:lists>"

    ccXML = ccXML + "<w:listDef w:listDefId='999'><w:plt w:val='SingleLevel'/><w:lvl w:ilvl='0'><w:start w:val='1'/><w:nfc w:val='23'/><w:pStyle w:val='mybullet'/><w:lvlText w:val='" + Chr(183) + "'/><w:lvlJc w:val='left'/><w:pPr><w:ind w:left='360' w:hanging='360'/></w:pPr><w:rPr><w:rFonts w:ascii='Symbol' w:h-ansi='Symbol' w:hint='default'/><w:color w:val='auto'/></w:rPr></w:lvl></w:listDef>"

    ccXML = ccXML + "<w:list w:ilfo='99'><w:ilst w:val='999'/></w:list>"

    ccXML = ccXML + "</w:lists>"

    ccXML = ccXML + "<w:body>"

    ccXML = ccXML + "<w:p><w:pPr><w:listPr><w:ilvl w:val='0'/><w:ilfo w:val='99'/><wx:t wx:val='" + Chr(183) + "'/><wx:font wx:val='Symbol'/></w:listPr></w:pPr><w:r><w:t>Sentence 1</w:t></w:r></w:p>"

    ccXML = ccXML + "<w:p><w:pPr><w:listPr><w:ilvl w:val='0'/><w:ilfo w:val='99'/><wx:t wx:val='" + Chr(183) + "'/><wx:font wx:val='Symbol'/></w:listPr></w:pPr><w:r><w:t>Sentence 2</w:t></w:r></w:p>"

    ccXML = ccXML + "</w:body>"

    ccXML = ccXML + "</w:wordDocument>"

    Selection.Range.InsertXML ccXML

    End Sub


    Peter Jamieson
    • Marked as answer by Diran Monday, May 23, 2011 2:46 PM
    Sunday, May 22, 2011 1:29 PM

All replies

  • Hi Diran,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please tell me which kind of project you created to handle your issue? So that I can reproduce it.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 13, 2011 3:30 AM
    Moderator
  • Hi Tom,

    Sure thing.  It is a Office 2007 Word Document project.

    I placed a plain text content control in the document as well as a rich text content control and then in the startup I have placed the code block seen in my above post.

    To make sure that it is possible to create lists within a content control, I then created a regular word document using Word and dropped a content control in and placed in text and formatted it to be an unordered list. It showed just fine and the document.xml contained what I am using in the code sample as well.

    I appreciate the time and hope to get a resolution with this issue.

    Thanks,

    -Diran

    Friday, May 13, 2011 11:42 AM
  • Hi Diran,

     

    I think what my puzzle is the mean of  <<The text that should be bulleted is shown but no bullet points at all>>. You Xml string is a completely Word document format. If you insert it in the document It will show and no bulleted we can see (If you try to insert it into context control’s range it will raise an exception). So, Would you please clarify the goal of the  <<The text that should be bulleted is shown but no bullet points at all>>?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 3:37 AM
    Moderator
  • What I'm trying to achieve is text to the effect of the following:

     

    Sentence 1

    • point 1
    • point 2

    Inside of a content control.  The only way that I've been able to insertXML into a content control has been if I use the sample provided above. I know the XML above is only set up for a bulleted approach as I used the WordXML format for creating a bulleted list. It just doesn't retain the bulleted values.

    Tuesday, May 17, 2011 11:58 AM
  • HI Diran

    I think the issue here is that a plain text content control cannot support bullet points. Bullet points = formatting = not plain text...


    Cindy Meister, VSTO/Word MVP
    Wednesday, May 18, 2011 6:35 AM
    Moderator
  • As I stated in the OP, I have both a Rich Text Content Control and a Plain Text Content Control.  It doesn't work for either.  I try each approach with both controls to see limitation.

    So, I have a Rich Text Content Control in there as well and it still doesn't work.

    Wednesday, May 18, 2011 12:36 PM
  • Hi Diran

    Mmm. The best test for something like this is to try it without a content control, because only then can you deterimine wihether it's a problem with the OpenXML or the content control. I just tried it outside a content control and got the same result. But I noticed something in a addition:

    Your XML isn't inserting a paragraph and the style isn't being applied. Bullet formatting is paragraph level. So if you don't insert something in its own paragraph it will take on the formatting of the paragraph into which it's being inserted.

    I'm guessing this is where the "disconnect" is occurring. I suspect your WordOpenXML requires the style definition for List Paragraph AND possibly the numbering definition. But I've never really worked with Word's XML vocabularies at this level, so it's just an educated guess.

    The OpenXML specialists probably know more about this, so I'm going to move the discussion to the Open XML forum where you're more likely to find people with expertise. You can also check the information and the forums at openXMLdeveloper.org.


    Cindy Meister, VSTO/Word MVP
    Thursday, May 19, 2011 12:59 PM
    Moderator
  • I'm pretty sure the <w:p> part is adding the paragraph.

     

    In any event can someone in these forums help me with the XML needed to accomplish my goal?

     

    Thanks!

    Thursday, May 19, 2011 2:17 PM
  • AFAICS the following will insert a couple of bulleted paras. it's in VBA so you'll need to translate back. As you will see, the key is that you have to "connect" the paragraphs to a listDef via an ilfo. However, without a lot more research it would be difficult to determine how reliable the insertion technique is, because when you are inserting into Word (rather than into an XML document) Word has to merge the XML you are inserting with what's there. I think it will probably create a new listDef and renumber the listDefs, but I'm not sure. So it's probably better to insert using the Object Model instead of XML if you can.

    AFAICS a lot of work has been done on numbering and bullets in the last couple of versions of Word -

     a. Word itself now seems to favour MultiLevel lists. I'm not sure it ever creates a SingleLevel list like this any more.

     b. there is doubtless stuff that you can do in Word 2007/2010 that canot be represented in the Word 2003 ML we're using here.

     c. there seems to be quite a lot of flexibility. e.g., here I do not specify a paragraph style. But you can, and you can connect that to the Listdef instead.

     

    Sub insertSomeBullets()

    ' inserts a couple of bulleted paras into the Selection

    ' select the CC's text, and run it.

    Dim ccXML As String

    ccXML = "<?xml version='1.0' standalone='yes'?>"

    ccXML = "<w:wordDocument xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml' xmlns:wx='http://schemas.microsoft.com/office/word/2003/auxHint' w:macrosPresent='no' w:embeddedObjPresent='no' w:ocxPresent='no' xml:space='preserve'>"

    ccXML = ccXML + "<w:lists>"

    ccXML = ccXML + "<w:listDef w:listDefId='999'><w:plt w:val='SingleLevel'/><w:lvl w:ilvl='0'><w:start w:val='1'/><w:nfc w:val='23'/><w:pStyle w:val='mybullet'/><w:lvlText w:val='" + Chr(183) + "'/><w:lvlJc w:val='left'/><w:pPr><w:ind w:left='360' w:hanging='360'/></w:pPr><w:rPr><w:rFonts w:ascii='Symbol' w:h-ansi='Symbol' w:hint='default'/><w:color w:val='auto'/></w:rPr></w:lvl></w:listDef>"

    ccXML = ccXML + "<w:list w:ilfo='99'><w:ilst w:val='999'/></w:list>"

    ccXML = ccXML + "</w:lists>"

    ccXML = ccXML + "<w:body>"

    ccXML = ccXML + "<w:p><w:pPr><w:listPr><w:ilvl w:val='0'/><w:ilfo w:val='99'/><wx:t wx:val='" + Chr(183) + "'/><wx:font wx:val='Symbol'/></w:listPr></w:pPr><w:r><w:t>Sentence 1</w:t></w:r></w:p>"

    ccXML = ccXML + "<w:p><w:pPr><w:listPr><w:ilvl w:val='0'/><w:ilfo w:val='99'/><wx:t wx:val='" + Chr(183) + "'/><wx:font wx:val='Symbol'/></w:listPr></w:pPr><w:r><w:t>Sentence 2</w:t></w:r></w:p>"

    ccXML = ccXML + "</w:body>"

    ccXML = ccXML + "</w:wordDocument>"

    Selection.Range.InsertXML ccXML

    End Sub


    Peter Jamieson
    • Marked as answer by Diran Monday, May 23, 2011 2:46 PM
    Sunday, May 22, 2011 1:29 PM