2016-08-17 33 views
-1

Env:Windows 10 64位,Visual Studio express 2013.我已經安裝了Microsoft Office 2013和2003.並且已經添加Microsoft Office 11.0 object libraryMicrosoft Word 11.0 object library參考。但沒有工作。 enter image description here enter image description here試圖通過Microsoft Word 11.0對象庫使用Microsoft.Office.Interop.Word導致編譯錯誤

我已經試過了,如果我改變Microsoft Word 11.0 object libraryMicrosoft Word 15.0 object library,一切都是OK.But我需要爲Word 2003中工作,所以,我應該怎麼辦?

代碼:

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Text; 
using Word = Microsoft.Office.Interop.Word; 

namespace readDOC { 
    class Program { 
     static void Main(string[] args) { 

      string cd = Directory.GetCurrentDirectory(); 
      Word.Application word = new Word.Application(); 
      List<string> dirs = new List<string>(); 
      foreach (string dir in Directory.GetFiles(@".", "*.doc?")) { 
       if (dir.IndexOf('~') == -1) { 
        dirs.Add(cd + dir.Substring(1)); 
       } 
      } 
      foreach (string fn in dirs) { 
       Word.Document docs = word.Documents.Open(fn); 
       try { 
        docs.Protect(Word.WdProtectionType.wdAllowOnlyReading); 
        Console.WriteLine(@"OK:{0}.", fn); 
       } catch (Exception e) { 
        Console.WriteLine(@"NOT OK:{0}.", fn); 
       } finally { 
        docs.Close(); 
       } 
      } 
      word.Quit(); 
      Console.WriteLine("Press any key to finish"); 
      Console.ReadLine(); 
     } 
    } 
} 
+2

什麼不行?什麼是錯誤?請發佈* full *例外文本,即使用'Exception.ToString()'。對於所有我們知道你可能會得到一個文件未找到錯誤 –

+0

順便說一句你不能*打開一個特定的版本,[這裏解釋](http://stackoverflow.com/questions/12073152/how-to-open-特定版本的字-2007-2010-在-Excel文件)。這不是特定於單詞的,這就是COM/OLE的工作原理。每個COM應用程序都會註冊所有以前的界面,所以當你詢問11.0時,你會得到註冊的應用程序,即Word 2013. –

+0

爲什麼你想要與2003進行互操作呢?您不需要那樣來生成'doc'而不是'docx'文件 - 您可以指定要通過互操作保存的格式。此外,很少有人仍然使用舊格式。 'docx'已經7歲了。你可以在沒有安裝Word的情況下使用OpenXML SDK生成'docx'文件 –

回答

1

審查,我相信那是因爲你不使用互操作(擴展)的問題後。

請確保您添加的是2003年將您的構建平臺目標設置爲x86。 2003年沒有x64支持。這是我的猜測。我無法測試2003年,因爲這是我的工作機器。

我希望我的建議能指出你朝着正確的方向。