vsto,C# 生成段落号

查了一些资料,好用的基本使用的都是spire,但这个对于我的使用方式来说不太合适,所以最终没有使用,下面的代码做个记录 

    using Word = Microsoft.Office.Interop.Word;

    Word.Application app = Globals.ThisAddIn.Application;
    Word.Document doc = app.ActiveDocument;

    Word.ListTemplate template =  doc.ListTemplates.Cast<Word.ListTemplate>().FirstOrDefault(info=>info.Name.Equals("paragraphNumber"))??doc.ListTemplates.Add(true, "paragraphNumber");    // 获取或新建模版
    Word.ListLevel level = template.ListLevels[1];
    level.NumberFormat = @"[%1]";    // 设置格式
    level.NumberStyle = Word.WdListNumberStyle.wdListNumberStyleArabicLZ3;    // 设置样式
    template.OutlineNumbered = true;

    int count = doc.Paragraphs.Count;
    Word.Range range;

    for (int i = 1; i <= count; i++)
    {
        range = doc.Paragraphs[i].Range;
        if (range.Frames.Count > 0)
        {
            continue;
        }

        text = range.Text;
        title = Regex.Replace(text, "[\\s\\a\\p{P}]", "");

        if (title.StartsWith("aaa"))    
        {
            isDescription = false;
            continue;
        }
        else if (title.StartsWith("ccc"))    
        {
            isDescription = true;
            continue;
        }        
        if (isDescription)    // 只保留ccc至aaa中间部分内容
        {
            if (doc.Paragraphs[i].Alignment == Word.WdParagraphAlignment.wdAlignParagraphCenter)    // 居中的跳过
            {
                continue;
            }
            if (string.IsNullOrWhiteSpace(text))    // 空白的跳过
            {
                continue;
            }
            range.ListFormat.ApplyListTemplate(template, true);    // 设置模版,编号连续
            range.ListFormat.ListLevelNumber = 1;
        }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值