[轉]關閉word進程


命名空間 :System.Diagnostics

      以前在word的時候,經常碰到word進程產生一大堆,怕關錯了,把用戶自己打開的word也關閉,一直搞忽悠,今天上網花了10塊錢,下了個文件,給我了一點啟發,總於可以實現只關閉程序中打開的,,,我自己測試了,是可以實現的,各位也可以copy去試試,不行的話,給我留言。。。

 

               //以下的定義為打開或保存word文件時,需要用到得參數

                object confirmConversions = Type.Missing;
                object readOnly = Type.Missing;
                object addToRecentFiles = Type.Missing;
                object passwordDocument = Type.Missing;
                object passwordTemplate = Type.Missing;
                object revert = Type.Missing;
                object writePasswordDocument = Type.Missing;
                object writePasswordTemplate = Type.Missing;
                object format = Type.Missing;
                object encoding = Type.Missing;
                object visible = Type.Missing;
                object openConflictDocument = Type.Missing;
                object openAndRepair = Type.Missing;
                object documentDirection = Type.Missing;
                object noEncodingDialog = Type.Missing;
                object missing = Type.Missing;
                object filename = Filename;
                object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; //解決normal.dot問題


                Microsoft.Office.Interop.Word._Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word._Document wordDoc = null;
                try
                {
                     
                    //打開Word文檔對象  
                    wordDoc = wordApp.Documents.Open(ref filename, ref confirmConversions, ref readOnly, ref addToRecentFiles, ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument, ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openConflictDocument, ref openAndRepair, ref documentDirection, ref noEncodingDialog);
                    wordDoc.AcceptAllRevisions(); //接收word中所有的修訂
                    wordDoc.Save();//保存
                    wordDoc.Close(ref missing, ref missing, ref missing);
                    wordApp.Application.Quit(ref saveOption, ref missing, ref missing);
                }
                catch { }
                finally
                {

                    //殺死打開的word進程
                    Process myProcess = new Process();
                    Process[] wordProcess = Process.GetProcessesByName("winword");
                    try
                    {
                        foreach (Process pro in wordProcess) //這里是找到那些沒有界面的Word進程
                        {
                           IntPtr ip= pro.MainWindowHandle;

                           string str = pro.MainWindowTitle; //發現程序中打開跟用戶自己打開的區別就在這個屬性
                            //用戶打開的str 是文件的名稱,程序中打開的就是空字符串
                            if (string.IsNullOrEmpty(str))
                            {
                                pro.Kill();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }

                }
            }
            catch (Exception ex)
            {
                bl = false;
            }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM