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("自动备案成功");

        }

 

posted @ 2019-04-19 14:08  .Alive  阅读(734)  评论(0编辑  收藏  举报