获取打开的Word文档

2018-06-18 01:26:17来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 1      using Word = Microsoft.Office.Interop.Word;
       int _getApplicationErrorCount=0;
       bool _isMsOffice = true;
       /// <summary>
 2         /// 获取最后一个当前处于活动状态的Word进程
 3         /// </summary>
 4         /// <param name="isLog">是否将检测到Office版本号写进日志,可根据项目需要删除该功能</param>
 5         /// <returns></returns>
 6         public static Word.Application GetWordApplication(bool isLog)
 7         {
 8             try
 9             {
10                 Word.Application wdApp = null;
11                 bool log = isLog;
12                 Thread.Sleep(100);
13                 if (Process.GetProcessesByName("WINWORD").ToList().Count > 0)
14                 {
15                     _isMsOffice = true;
16                     wdApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
17                     _getApplicationErrorCount = 0;
18                     if (log)
19                         Trace.WriteLine("检测到使用的MS Office " + wdApp.Version);
20                 }
21                 else if (Process.GetProcessesByName("wps").ToList().Count > 0)
22                 {
23                     _isMsOffice = false;
24                     if (Type.GetTypeFromProgID("Kwps.Application") != null)
25                     {
26                         wdApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Kwps.Application");
27                         _getApplicationErrorCount = 0;
28                         if (log)
29                             Trace.WriteLine("检测到使用的WPS Office " + wdApp.Version + ";VBA V9");
30                     }
31                     else if (Type.GetTypeFromProgID("wps.Application") != null)
32                     {
33                         wdApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Wps.Application");
34                         _getApplicationErrorCount = 0;
35                         if (log)
36                             Trace.WriteLine("检测到使用的WPS Office " + wdApp.Version + ";VBA V8");
37                     }
38                     else
39                     {
40                         MessageBox.Show("当前WPS 不支持VBA");
41                     }
42                 }
43                 else
44                 {
45                     wdApp = null;
46                 }
47                 return wdApp;
48             }
49             catch (Exception ex)
50             { 
52                 _getApplicationErrorCount++;
53                 if (_getApplicationErrorCount <= 5) //检测失败重试5次
54                 {
55                     Thread.Sleep(200);
56                     return GetWordApplication(false);
57                 }
58                 else
59                 {
60                     Trace.WriteLine("Get word application failed!" + ex.Message + ex.StackTrace);
61                     _getApplicationErrorCount = 0;
62                     return null;
63                 }
64             }
65         }

注:

  调用是可使用Word.Application wdApp = GetWordApplication(true);

  获取该进程中的所有文档可用:Word.Documents docs = wdApp.Documents;

  转载请注明出处,谢谢!

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:asp.net mvc 验证码

下一篇:.NET 基础 一步步 一幕幕[数组、集合、异常捕获]