C#操作word文档,复制指定页面至新的word

 word类库目前用得较多的有a. Free Spire.Doc for .NET  b. NPOI  c. Microsoft.Office.Interop.Word

其中Spire免费版的有页数限制,会生成水印。本文根据实际需求,采用vs2017下载的Microsoft.Office.Interop.Word操作word文档。

用户可通过右键项目,点击NuGet程序包,搜索word找到Microsoft.Office.Interop.Word,本文安装的版本为12.0.0  如图:

安装完成后,即添加至引用了,使用时只需引入对应命名空间using MSWord = Microsoft.Office.Interop.Word;就可使用了,如图:

 下面直接上完整代码,

Object Nothing = System.Reflection.Missing.Value;
            Object objDocType = WdDocumentType.wdTypeDocument;
            Object readOnly = false;
            Object isVisible = true;
            Object sourceDocPath = @"D:\a.docx";
            Application wordApp = new ApplicationClass();
            Document newWordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            Document openWord = wordApp.Documents.Open(ref sourceDocPath, ref Nothing, ref readOnly,
                        ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing,
                        ref Nothing, ref Nothing, ref Nothing);
            Object what = MSWord.WdGoToItem.wdGoToPage;
            Object which = MSWord.WdGoToDirection.wdGoToFirst;
            Object count = 7;
            Object count2 = 8;
            try
            {
                MSWord.Range startRange = wordApp.Selection.GoTo(ref what, ref which, ref count, ref Nothing);
                MSWord.Range endRange = wordApp.Selection.GoTo(ref what, ref which, ref count2, ref Nothing);
                endRange.SetRange(startRange.Start, endRange.End - 1);
                endRange.Select();
                wordApp.Selection.Copy();
                newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                // 插入换行符 WdBreakType.wdTextWrappingBreak
                Object type = WdBreakType.wdSectionBreakNextPage; // 分节符在下一页
                Object type2 = WdBreakType.wdLineBreakClearLeft; // 换行符
                // newWordDoc.Sections[1].Range.InsertBreak(ref type2);
                newWordDoc.Sections[1].Range.InsertBreak(ref type);
                newWordDoc.Sections[1].Range.InsertBreak(ref type);
                Object count3 = 5;
                Object count4 = 6;
                MSWord.Range startRange2 = wordApp.Selection.GoTo(ref what, ref which, ref count3, ref Nothing);
                MSWord.Range endRange2 = wordApp.Selection.GoTo(ref what, ref which, ref count4, ref Nothing);
                endRange2.SetRange(startRange2.Start, endRange2.End - 1);
                endRange2.Select();
                wordApp.Selection.Copy();
                newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault);

                Object outputFileName = "d:\\test3.doc";
                Object fileFormat = MSWord.WdSaveFormat.wdFormatDocument97;
                newWordDoc.SaveAs(ref outputFileName,
                 ref fileFormat, ref Nothing, ref Nothing,
                 ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                 ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                 ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                newWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                openWord.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                GC.Collect();
            }
            catch (Exception)
            {
                newWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                openWord.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                GC.Collect();
                Console.WriteLine("提取异常!");
                Console.ReadKey();
                return;
            }

 注释:本文提取的是第7页和第5页,将提取的页码粘贴至新的word文档中,关键代码就下面这几行:

Object count3 = 5; // 第5页
Object count4 = 6; // 第6页
MSWord.Range startRange2 = wordApp.Selection.GoTo(ref what, ref which, ref count3, ref Nothing);
MSWord.Range endRange2 = wordApp.Selection.GoTo(ref what, ref which, ref count4, ref Nothing);
endRange2.SetRange(startRange2.Start, endRange2.End - 1);  //-1为到第5页未尾字符索引位置
endRange2.Select();  // 选择复制区域
wordApp.Selection.Copy(); // 复制
newWordDoc.Sections[1].Range.PasteAndFormat(WdRecoveryType.wdPasteDefault); // 粘贴至新文档

 本文复制的由于不是连续的页,用了两次分节符才能成功,大家有其它改进的可以一起讨论

newWordDoc.Sections[1].Range.InsertBreak(ref type);
newWordDoc.Sections[1].Range.InsertBreak(ref type);

 温馨提示:在程序结束前要及时释放对象,不然在打开word文档时可能会卡死或异常

newWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
openWord.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
GC.Collect();

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值