2010-09-21 65 views
0

以下代碼使用COM打開和關閉Word文檔;它不會產生任何錯誤。使用COM關閉文檔後Word菜單被卡住

但是,如果在運行代碼之前將Word最大化,則在程序運行後,Word菜單不會對鼠標或鍵盤操作做出反應。

public static void Main(string[] args) 
{ 
    try 
    { 
     var wordDocument = GetWordDocumentName(); 
     var wordApplication = GetWordApplication(); 

     // Open the document. 
     wordApplication.Documents.Open((object)wordDocument); 

     // This is what causes the problem. 
     // Close the document. 
     wordApplication.ActiveDocument.Close(); 

     Console.WriteLine("Can you use Word menus or ribbon?"); 
    } 
    catch (Exception ex) 
    { 
     WriteException(ex); 
    } 

    Console.WriteLine(); 
    Console.WriteLine("Press any key to continue..."); 
    Console.ReadKey(); 
} 

有兩種方法來解決這個錯誤。

  • 確保單詞在運行代碼之前沒有最大化。
  • 選擇另一個應用程序,然後返回到Word。

到目前爲止我已經能夠複製在以下環境問題:

  • Windows 7的& Word 2003中
  • Windows 7的& Word 2007中
  • Windows Vista中& Word 2003中

該問題在Windows Server 2008上不存在Word 2003中

整個代碼如下:

namespace WordMenuSharp 
{ 
    using System; 
    using System.IO; 
    using Microsoft.VisualBasic; 

    public class Program 
    { 
     public static void Main(string[] args) 
     { 
      try 
      { 
       var wordDocument = GetWordDocumentName(); 
       var wordApplication = GetWordApplication(); 

       // Open the document. 
       wordApplication.Documents.Open((object)wordDocument); 

       // This is what causes the problem. 
       // Close the document. 
       wordApplication.ActiveDocument.Close(); 

       Console.WriteLine("Can you use Word menus or ribbon?"); 
      } 
      catch (Exception ex) 
      { 
       WriteException(ex); 
      } 

      Console.WriteLine(); 
      Console.WriteLine("Press any key to continue..."); 
      Console.ReadKey(); 
     } 

     private static string GetWordDocumentName() 
     { 
      var fileName = Path.Combine(
       Directory.GetCurrentDirectory(), 
       "WordMenuTest.doc" 
      ); 

      if (!File.Exists(fileName)) 
      { 
       throw new FileNotFoundException(
        fileName + " must be created." 
       ); 
      } 

      return fileName; 
     } 

     private static 
      Microsoft.Office.Interop.Word.Application GetWordApplication() 
      { 
       Microsoft.Office.Interop.Word.Application wordApplication; 

       try 
       { 
        wordApplication = 
         (Microsoft.Office.Interop.Word.Application)Interaction 
          .GetObject(null, "Word.Application"); 
       } 
       catch (Exception ex) 
       { 
        throw new Exception(
         "Error while getting Word, have you opened it." + 
         Environment.NewLine + 
         ex.Message, 
         ex 
        ); 
       } 

       if (wordApplication.Documents.Count == 0) 
       { 
        throw new Exception(
         "A file must already be open in Word." 
        ); 
       } 

       return wordApplication; 
      } 

     private static void WriteException(Exception ex) 
     { 
      Console.ForegroundColor = ConsoleColor.Red; 
      Console.WriteLine(); 
      Console.WriteLine(ex.Message); 
      Console.ForegroundColor = ConsoleColor.White; 
     } 
    } 
} 
+0

當時可能會把注意力放在其他地方。緊接着'Documents.Open((object)wordDocument)'之後,如果調用'Documents((object)wordDocument),它還會這樣做嗎。激活'?或者如果你只是做了'Documents(object)wordDocument).Close()'? – 2010-10-18 22:21:49

回答

0

聽起來像是你可能已經發現(另一個)錯誤在Word中。

您是否嘗試過類似於最小化,然後在關閉活動文檔後再重新生成Word應用程序對象?

也可能嘗試將其設置爲Visible = false,然後Visible = true。

你可能會得到一個屏幕閃光,但它也可能修復功能區/等。