none
multi-level list in Microsoft.Office.Interop.Word RRS feed

  • Question

  • hi everyone.

    What is the best way to create subj in c#?

    I tried to do this applying styles but I cant find such style in WdBuiltinStyle .

    may be someone has solved this problem before & have code example?

    Thursday, May 26, 2011 8:56 AM

Answers

  • Hi Artem

    Here's some sample code to give you an idea how to procede:

        private void btnMultilevelList_Click(object sender, EventArgs e)
        {
          Word.Document doc = wdApp.ActiveDocument;
          Word.Range rng = doc.Content;
          object oTrue = true;
          object oFalse = false;
          object oListName = "TreeList";
          Word.ListTemplate lstTemp = doc.ListTemplates.Add(ref oTrue, ref oListName);
          int i;
    
          rng.Text = "Level 1\rLevel 1.1\rLevel 1.2\rLevel 2\rLevel 2.1\rLevel 2.1.1";
    
          i = 1;
          lstTemp.ListLevels[i].NumberFormat = "%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          i = 2;
          lstTemp.ListLevels[i].NumberFormat = "%" + (i - 1).ToString() + ".%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          i = 3;
          lstTemp.ListLevels[i].NumberFormat = "%" + (i - 2).ToString() + "%" + (i - 1).ToString() + ".%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          object oListApplyTo = Word.WdListApplyTo.wdListApplyToWholeList;
          object oListBehavior = Word.WdDefaultListBehavior.wdWord10ListBehavior;
    
          rng.ListFormat.ApplyListTemplate(lstTemp, ref oFalse, ref oListApplyTo, ref oListBehavior);
    
        }
    

    Cindy Meister, VSTO/Word MVP
    Tuesday, May 31, 2011 9:57 AM
    Moderator

All replies

  • Which version of Word? The details of working with numbering vary a bit from version to version.

    Also, can you give us a bit more detail about the kind of project you're working on? More specifically, the circumstances in which you'd require the numbering?

    For getting started: record  a macro for the steps you follow to create the list in the Word UI, as an end-user. That will give you an idea of the basic syntax. But for details/optimization, we need to know the version of Word involved.


    Cindy Meister, VSTO/Word MVP
    Thursday, May 26, 2011 10:50 AM
    Moderator
  • I use Word 2007 and C# .NET framework 4.

    I have treeView control in the project and need to generate multilevel list in word document (.docx) according to this treeView taking into account its hierarchy and numbering each node.

    The result should be something like this

    1. "node 1"

        1.1 "node 1.1"

        1.2 "node 1.2"

    2. "node 2"

        2.1 "node 2.1"

    The deepest nesting level is 4

    Friday, May 27, 2011 6:30 AM
  • Hi Artem

    Here's some sample code to give you an idea how to procede:

        private void btnMultilevelList_Click(object sender, EventArgs e)
        {
          Word.Document doc = wdApp.ActiveDocument;
          Word.Range rng = doc.Content;
          object oTrue = true;
          object oFalse = false;
          object oListName = "TreeList";
          Word.ListTemplate lstTemp = doc.ListTemplates.Add(ref oTrue, ref oListName);
          int i;
    
          rng.Text = "Level 1\rLevel 1.1\rLevel 1.2\rLevel 2\rLevel 2.1\rLevel 2.1.1";
    
          i = 1;
          lstTemp.ListLevels[i].NumberFormat = "%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          i = 2;
          lstTemp.ListLevels[i].NumberFormat = "%" + (i - 1).ToString() + ".%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          i = 3;
          lstTemp.ListLevels[i].NumberFormat = "%" + (i - 2).ToString() + "%" + (i - 1).ToString() + ".%" + i.ToString() + ".";
          lstTemp.ListLevels[i].NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabic;
          lstTemp.ListLevels[i].NumberPosition = wdApp.CentimetersToPoints(0.5f * (i - 1));
          lstTemp.ListLevels[i].TextPosition = wdApp.CentimetersToPoints(0.5f * i);
          object oListApplyTo = Word.WdListApplyTo.wdListApplyToWholeList;
          object oListBehavior = Word.WdDefaultListBehavior.wdWord10ListBehavior;
    
          rng.ListFormat.ApplyListTemplate(lstTemp, ref oFalse, ref oListApplyTo, ref oListBehavior);
    
        }
    

    Cindy Meister, VSTO/Word MVP
    Tuesday, May 31, 2011 9:57 AM
    Moderator
  • Hi Cindy,

    By trying your code sample, I end up having this :

       
    Level 1.   
    Level 1.1   
    Level 1.2   
    Level 2   
    Level 2.1   
    Level 2.1.1

    To add some indentation to the different elements, try using Paragraph.IndentWhithChar(int value);

    The value can be negative to outdent. Some kind of OutdentWithChar.

    Regards,

    Linvi



    • Edited by linvỉ Thursday, March 28, 2013 4:50 PM
    Wednesday, March 27, 2013 5:18 PM
  • Hi Linvi

    It's always a very bad idea to piggy-back on a very old discussion that's already been answered - your "visibility" is poor. Much better to start a new thread with a link to any relevant, older discussions...

    If you look at my code, you'll see that .NumberPosition and .TextPosition are the same for all three levels. That means all levels will align in the same place. You need to set these values for the positions you want numbered elements to have when you apply this List template.

    DO NOT try to set the indents after the fact as the formatting will NOT be written back to the List Template. This means that any subsequent use of the List template will result in different positional formatting.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, March 29, 2013 11:05 AM
    Moderator
  • There is no effect on result even İf I give a different number for position.

    But range text value is important. \t create next level.
    • Proposed as answer by erikfx4 Sunday, March 12, 2017 7:32 PM
    Monday, February 24, 2014 1:17 PM
  • velikoksal has it!  Here's how simple it can be:


    After making a word instance and creating or opening a document and defining paragraphs then adding a paragraph (respond to this for that code):

    Range pipe = para.Range;
    pipe.Text = "Piping\r\tGalvanized Steel Piping General Information and Specifications\r";
    pipe.Font.Name = "Arial";
    pipe.Font.Size = 10;
    pipe.ListFormat.ApplyListTemplate(list, ref contList, ref applyTo, ref listBehav);

    comes out to:

    1) Piping 
           i) Galvanized Steel Piping General Information and Specifications 


    • Edited by erikfx4 Sunday, March 12, 2017 7:37 PM
    • Proposed as answer by erikfx4 Sunday, March 12, 2017 7:38 PM
    Sunday, March 12, 2017 7:37 PM