C# word 通过书签拆分文档。Microsoft.Office.Interop.Word

 1  public static ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
 2 
 3         /// <summary>
 4         /// 拆分word
 5         /// </summary>
 6         public static void CovertWord(string filePath, string name)
 7         {
 8             
10             object missing = System.Reflection.Missing.Value;12             try
13             {15                 Document doc = ReadDocument(filePath);//读取文件
16 
17                 for (int i = 1; i <= doc.Bookmarks.Count; i++)
18                 {
19                     object index = i;
20                     Bookmark bm = doc.Bookmarks.get_Item(ref index);
21                     if (bm.Name == "技术报告" || bm.Name == "结果报告")
22                     {
23                         //复制书签内容
24                         bm.Range.Copy();
25                         //创建新文档储存复制内容
26                         Document docto = CreateDocument();
27                         docto.Content.Paste();
28                         //创建储存文件路径 
29                         object filename =  @"E:\1\" + name + bm.Name + ".docx";
30                         docto.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,
31                             ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
32                             ref missing, ref missing, ref missing, ref missing, ref missing);
33                         docto.Close(ref missing, ref missing, ref missing);36                     }
37                 }
38                 return listPath;
39 
40             }
41             catch (Exception ex)
42             {
43                 throw new Exception(ex.Message);
44             }
45             finally
46             {
47                 //清理回收站
48                 GC.Collect();
49                  //word.Quit(ref missing, ref missing, ref missing);
50             }
51 
52         }
 1 /// <summary>
 2         /// 读取word文档
 3         /// </summary>
 4         /// <param name="path">word文档路径</param>
 5         /// <returns></returns>
 6         public static Document ReadDocument(string path)
 7         {
 8             object missing = System.Reflection.Missing.Value;
 9             Type wordType = word.GetType();
10             Documents docs = word.Documents;
11             Type docsType = docs.GetType();
12 
13             object objDocName = path;
14             Document doc = (Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });
15 
16             return doc;
17         }

 

 1   /// <summary>
 2         /// 创建word文档
 3         /// </summary>
 4         /// <returns></returns>
 5         public static Document CreateDocument()
 6         {
 7             object missing = System.Reflection.Missing.Value;
 8             Document newdoc = word.Documents.Add(ref missing, ref missing, ref missing, ref missing);
 9             return newdoc;
10         }
public static void Main(string[] args)
        {
           var filePath = @"E:\1\test.docx";
            CovertWordView.CovertWord(filePath);
            Console.WriteLine("自动备案成功");

        }

 

转载于:https://www.cnblogs.com/longshanshan/p/10735613.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值