none
Associate heading styles with numbering levels RRS feed

  • Question

  • Hi Cindy,

    I am so glad to say that your code has helped me to solve my issue. 

    I have a doubt regarding ListLevels,

    Can we keep different heading styles for each different level?

    I have tried as below please follow the code as below 

    private void OnNumbering_Click(object sender, RibbonControlEventArgs e)
    {
        Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;
        Document Currentdoc = _CurrentDocument;
        Range rng = _CurrentRange;
        object oTrue = true;
        object oFalse = false;
        object oListName = "TreeList";
        ListTemplate lstTemp = Currentdoc.ListTemplates.Add(ref oTrue, ref oListName);
        for (int level = 1; level <= 5; level++)
        {
           switch (level)
           {
            case 1: lstTemp.ListLevels[level].NumberFormat = "%" + level.ToString() + ".";
    lstTemp.ListLevels[level].Application.Selection.ParagraphFormat.set_Style(WdBuiltinStyle.wdStyleHeading1);
                                    break;
            case 2: lstTemp.ListLevels[level].NumberFormat = "%" + (level - 1).ToString() + ".%" + level.ToString() + ".";
                                    lstTemp.ListLevels[level].Application.Selection.ParagraphFormat.set_Style(WdBuiltinStyle.wdStyleHeading2);
                                    break;
             case 3: lstTemp.ListLevels[level].NumberFormat = "%" + (level - 2).ToString() + ".%" + (level - 1).ToString() + ".%" + level.ToString() + ".";
                                    lstTemp.ListLevels[level].Application.Selection.ParagraphFormat.set_Style(WdBuiltinStyle.wdStyleHeading3);
                                    break;
             case 4: lstTemp.ListLevels[level].NumberFormat = "%" + (level - 3).ToString() + ".%" + (level - 2).ToString() + ".%" + (level - 1).ToString() + ".%" + level.ToString() + ".";
                                    lstTemp.ListLevels[level].Application.Selection.ParagraphFormat.set_Style(WdBuiltinStyle.wdStyleHeading4);
                                    break;
             case 5: lstTemp.ListLevels[level].NumberFormat = "%" + (level - 4).ToString() + ".%" + (level - 3).ToString() + ".%" + (level - 2).ToString() + ".%" + (level - 1).ToString() + ".%" + level.ToString() + ".";
    
    lstTemp.ListLevels[level].Application.Selection.ParagraphFormat.set_Style(WdBuiltinStyle.wdStyleHeading5);
                                    break;
                      }
                            lstTemp.ListLevels[level].NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
                            lstTemp.ListLevels[level].NumberPosition = app.CentimetersToPoints(0.5f * (level - 1));
                            lstTemp.ListLevels[level].TextPosition = app.CentimetersToPoints(0.5f * level);
                     }
    object oListApplyTo = WdListApplyTo.wdListApplyToWholeList;
                        object oListBehavior = WdDefaultListBehavior.wdWord10ListBehavior;
    lstTemp = lstgly.ListTemplates[5];
    rng.ListFormat.ApplyListTemplateWithLevel(lstTemp, ref oFalse, ref oListApplyTo, ref oListBehavior);
    }

    I have given different heading styles for differrent levels as shown above but, i am able to get only the last level heading style for all levels. Please guide me for this issue as i need different styles for different levels.

    Thanks in advance

    • Split by Cindy Meister MVPModerator Wednesday, October 30, 2013 2:55 PM From answered question http://social.msdn.microsoft.com/Forums/office/en-US/5ef5d39f-ad3d-4762-924f-b4b28f9f8bbd/multilevel-list-in-microsoftofficeinteropword?forum=worddev#d3cc5b35-4535-4f85-867c-fcd91476336d
    Wednesday, October 30, 2013 5:06 AM

Answers

  • I believe you're using the wrong property to assign a style to a ListLevel of a ListTemplate. In VBA-speak:

         lstTemp.ListLevels(level).LinkedStyle = "Heading 4"

    In C#, it appears the syntax would be something like:

        lstTemp.ListLevels[level].LinkedStyle = "Heading 1";

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 30, 2013 7:00 PM
    Moderator