2015-04-30 59 views
0

我正在使用C#。有了這段代碼,我試圖打開一個Word文檔(WINWORD)並獲取通知,當我打開特定文件或文件名時從軟件上已經接近。 現在,如果我從軟件中打開x.docx文件並打開另一個y.docx manualy並關閉y.docx文件,那麼我的程序就是x.docx,因爲它們具有相同的進程名稱WINWORD。 我已閱讀了很多文章,但找不到任何內容。是否有可能以任何方式做到這一點?如何從子進程定義具有相同進程ID的父進程

private void button1_Click(object sender, EventArgs e) 
     { 

      Process mydoc = new Process(); 
      mydoc.StartInfo.FileName = "op.docx"; 

      mydoc.StartInfo.WorkingDirectory = "C:\\"; 


      mydoc.Start(); 
      mydoc.WaitForExit(); 


      notifyIcon1.Visible = true; 
      notifyIcon1.Icon = new System.Drawing.Icon(Path.GetFullPath(@"C:\Users\Marios\Desktop\down.ico")); 

      notifyIcon1.BalloonTipTitle = "waiting..."; 
      notifyIcon1.BalloonTipText = "Word has has opened..."; 
      notifyIcon1.ShowBalloonTip(100); 

      Displaynotify(); 
     } 

    protected void Displaynotify() 
     { 
      try 
      { 
       var processes = Process.GetProcessesByName("WINWORD"); 
       notifyIcon1.Visible = true; 
       notifyIcon1.Icon = new System.Drawing.Icon(Path.GetFullPath(@"C:\Users\Marios\Desktop\down.ico")); 

       notifyIcon1.BalloonTipTitle = "exited."; 
       notifyIcon1.BalloonTipText = "Word has exited..."; 
       notifyIcon1.ShowBalloonTip(100); 
      } 
      catch (Exception ex) 
      { 
      } 
     } 

更新1與應答

我不得不改變我的代碼,現在我用這種方式打開它,但我仍然不能做我想做的鏈接

private void button1_Click(object sender, EventArgs e) 
     { 

     Word.Application oWord = new Word.Application(); 
    Microsoft.Office.Interop.Word.Document oDoc = oWord.Documents.Open("C:\\op.docx"); 
      oWord.Visible = true; 

      ((Word._Document)oDoc).Close(); 
      // ((Word._Document)oDoc).Close(); 
      //((Word._Application)oWord).Quit(); 
      //Displaynotify(); 
} 

回答

-1

你可以從您的C#應用​​程序中自動執行Word。在這種情況下,您將能夠檢查當前打開哪些文檔,並處理文檔關閉時觸發的Document類的Close事件。

有關C#中的更多信息和示例代碼,請參閱How to automate Microsoft Word to create a new document by using Visual C#。您也可以找到C# app automates Word (CSAutomateWord)示例項目很有幫助。

在這種情況下,不需要運行新的進程。所有的工作都可以使用Word自動完成。

+0

你能否用我的代碼寫下你的答案,看看怎麼辦? – marios

+0

看看我在帖子中提到的示例項目。 –

+0

我和我在第3步中出現錯誤,oWord不存在於內容中。任何接受此答案的方法都可以作爲示例或在您的代碼上使用我的代碼。您寫的示例項目並不是我想要的。 – marios