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.wdTextWrappingBreakObject 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();

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

  1. 解决把一篇word文档复制到另一篇word文档时, 更改标题格式

    问题: 两篇word 文档的标题格式不一致,比如一个标题在一篇文档中的样式是标题1,要将其拷贝到另一篇文档的标题3位置. 解决办法: 1.将word1中的标题1 改称标题3 参考:word2016文档 ...

  2. html word页面展示,word文档怎样调页面

    word文档如何调整页面?在页面设置对话框的"页边距2003版"选项卡中选择"方向"为横向或是纵向. Word中怎么设置一个页面显示4页 Word一页只显示四页 ...

  3. word文档粘贴到html,将Word 文档复制到Dreamweaver的方法

    将Word 文档复制到Dreamweaver的方法 一.用复制的办法. 1.打开Word 文档,复制文本. 2.在Dreamweaver 中,从"编辑"菜单中使用"粘贴& ...

  4. python-docx对Word文档的指定位置(批量)插入图片

    python-docx对Word文档的指定位置(批量)插入图片 任务 实现自动化办公,对请假条.docx文件实现自动插入请假人签名图片. 技术方案 1.python-docx python-docx是 ...

  5. java获取word固定位置的值_java 实现保存Word文档中指定位置的数据,又保存整篇文档...

    1:需求 用户在线编辑完word 文档后希望保存整篇文档, 同时把保存文档中指定位置的数据 2:方案 用 pageoffice  实现既保存Word文档中指定位置的数据,又保存整篇文档 4:核心思想及 ...

  6. Word处理控件Aspose.Words功能演示:在 Java 中将内容从 Word 文档复制到另一个文档

    Aspose API 支持旗下产品覆盖文档.图表.PDF.条码.OCR.CAD.HTML.电子邮件等各个文档管理领域,为全球.NET .Java.C ++等10余种平台开发人员提供丰富的开发选择. 在 ...

  7. Word文档在前台页面展示

    这篇文章主要是围绕如何实现Word文档在页面上进行预览,以及涉及到相关的技术点,和我们将会在这个功能上使用的插件. 插件:Aspose.Total: Aspose.Total是Aspose公司旗下的最 ...

  8. word文档中指定页加水印_如何在Microsoft Word文档中使用水印

    word文档中指定页加水印 A watermark is a faded background image that displays behind the text in a document. Y ...

  9. 如何将word文档放入html页面,怎么将Word文档转换为Web页面

    为了方便将Word文档在互联网上和局域网上发布,需要将文档保存为Web页面文件.将文档保存为Web页面,这种页面文件使用HTML文件格式.以下是学习啦小编为您带来的关于将Word文档转换为Web页面的 ...

最新文章

  1. ubuntu配置Android指南
  2. linux系统调用理解之摘录(3)
  3. 百度测试开发提前批一面面经
  4. TCP/UDP的接收缓冲区和发送缓冲区
  5. VASP、Lammps快速上手神器——MedeA软件
  6. ucgui button
  7. dbf 转mysql_DbfToMysql-DbfToMysql(Dbf数据转换Mysql工具)下载 v1.6官方版--pc6下载站
  8. 二元函数连续性、可导性及极限
  9. Python机器视觉--OpenCV进阶(核心)-边缘检测之SIFT关键点检测
  10. 流程图,NS图,伪代码
  11. IcedTea6 1.7.6、1.8.3和1.9.2补丁程序信息泄漏
  12. 简单总结里的结构化,成长型思维
  13. 人群计数:人群计数研究综述(2018.11)
  14. python:http.server --- HTTP 服务器
  15. php 小偷,php 小偷程序实例
  16. 状态空间平均建模——Flyback
  17. 苹果手机电越充越少怎么回事_羞羞电量插件app免费下载-羞羞手机电量插件v1.0 安卓版...
  18. 怎么购买苹果教育优惠产品?苹果在线商店购买教育优惠产品的具体流程
  19. 详解证券经纪业务流程
  20. 请用matlab语言计算一下多自由度无阻尼自由振动的固有频率

热门文章

  1. 1634D. Finding Zero
  2. 密码学五:RSA算法
  3. 服务sql server(MSSQLSERVER)意外停止。这发生了2次。
  4. 当你离开之后我真的哭了
  5. phicomm虚拟服务器,phicomm路由器设置dns服务器
  6. 计算机应用基全部概念,《计算机应用基》复习大纲.doc
  7. Flutter实战——实践是检验技术的唯一标准
  8. uniapp转微信小程序后Vue.prototype定义的全局变量在微信小程序页面无法直接访问
  9. 随机抽人名小程序_岛游网小程序1.0.1版本更新公告
  10. 如何规划地域和可用区(相关定义和分析)