通过查资料和不断的尝试,终于知道如何设置重新开始于了,目前知道的方法有两个:
关键:一定要对段落前面的编号做设置,否则不行(.SelectNumber())。
- 必须知道word中的编号style,对错误的编号重新应用即可。但是,缺点是要知道编号的style(可能可以先获取,没有深究)。
- 通过重新应用ListTemplate来解决,示例如下:
public static void CheckListNumber(Word.Document doc)
{
Word.Paragraph beforeParagrah = null;
int i = 0;
int allCount = doc.ListParagraphs.Count;
foreach (Word.Paragraph listPargraph in doc.ListParagraphs)
{
i++;
doc.Application.StatusBar = $"Checking ListItem Num...{i}/{allCount}";
//判断是否有listformat
Word.ListFormat listFormat = listPargraph.Range.ListFormat;
if (listFormat == null)
{
continue;
}
//判断是否是有序号的list
string listString = listFormat.ListString;
if (!Regex.IsMatch(listString, "[a-z,0-9]", RegexOptions.IgnoreCase))
{
continue;
}
//开始检查
if (beforeParagrah == null)
{
beforeParagrah = listPargraph;
continue;
}
else
{
int listValue = beforeParagrah.Range.ListFormat.ListValue;
Word.ListTemplate listTemplate = beforeParagrah.Range.ListFormat.ListTemplate;
if (listPargraph.OutlineLevel != Word.WdOutlineLevel.wdOutlineLevelBodyText &&
beforeParagrah.OutlineLevel == Word.WdOutlineLevel.wdOutlineLevelBodyText &&
listValue != 1)
{
AddComment(beforeParagrah.Range, $"此处编号有错,已由【{listValue}】修改为【1】!");
beforeParagrah.SelectNumber();
Word.Selection sel = beforeParagrah.Range.Application.Selection;
listTemplate.ListLevels[1].StartAt = 1;
sel.Range.ListFormat.ApplyListTemplateWithLevel(listTemplate, false, Word.WdListApplyTo.wdListApplyToWholeList, Word.WdDefaultListBehavior.wdWord10ListBehavior);
}
beforeParagrah = listPargraph;
}
}
}