Word中编号重新开始于 设置方法

通过查资料和不断的尝试,终于知道如何设置重新开始于了,目前知道的方法有两个:

关键:一定要对段落前面的编号做设置,否则不行(.SelectNumber())。

  1. 必须知道word中的编号style,对错误的编号重新应用即可。但是,缺点是要知道编号的style(可能可以先获取,没有深究)。
  2. 通过重新应用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;
                }
            }
        }

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值