问题
I'm trying to assign numbering to multiple heading styles in Word, so that I get content like the following:
6. <Heading 1>
6.1. <Heading 2>
6.1.1. <Heading 3>
6.2. <Heading 2>
6.2.1. <Heading 3>
6.3. <Heading 2>
You can do this pretty easily in standard Word by using the second multilevel numbering style (ignoring 'None'), the one that looks like this:
1.
1.1.
1.1.1.
But I'm trying to do this dynamically, by applying the second multilevel numbering style in code, and it comes out like this:
6. <Heading 1>
1.1 <Heading 2>
1.1.1 <Heading 3>
7. <Heading 1>
1.2 <Heading 2>
1.1.2 <Heading 3>
If I apply the second multilevel numbering style manually after this, it changes to the desired output.
Here is my code (a bit simplified but not by much).
ListGallery gallery = doc.Application.ListGalleries[WdListGalleryType.wdOutlineNumberGallery];
ListTemplate numberedTemplate = gallery.ListTemplates[2];
for (int i = 1; i < 10; i++)
{
Style style = doc.Styles["Heading " + i];
style.LinkToListTemplate(numberedTemplate, i);
}
回答1:
Using ListGalleries is not reliable/robust. In the case of indexes (choosing a number format from the list in the UI) you can't be sure the content of the list is always the same - that drawback is obvious.
The problem with the built-in galleries, such as you're using, is that this particular one has a "bug" (as it did in the UI when first released): levels 2 and 3 don't have the "Restart List After" setting activated, so you get the described behavior.
For that reason, it's better to record a macro while creating a custom list, preferably using "Define New List Style" from the outline numbering menu. You can then use the code anywhere.
Defining a named List Style will give you a named List Template, which means it can be re-used and managed directly. (In your code you use an index value to reference a ListTemplate, which can go wrong if the one you intend to use is not the second one in the collection.)
来源:https://stackoverflow.com/questions/37353021/multiple-heading-styles-using-the-same-multilevel-list-style