-1
Env:Windows 10 64位,Visual Studio express 2013.我已經安裝了Microsoft Office 2013和2003.並且已經添加Microsoft Office 11.0 object library
和Microsoft Word 11.0 object library
參考。但沒有工作。
試圖通過Microsoft Word 11.0對象庫使用Microsoft.Office.Interop.Word導致編譯錯誤
我已經試過了,如果我改變Microsoft Word 11.0 object library
到Microsoft 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();
}
}
}
什麼不行?什麼是錯誤?請發佈* full *例外文本,即使用'Exception.ToString()'。對於所有我們知道你可能會得到一個文件未找到錯誤 –
順便說一句你不能*打開一個特定的版本,[這裏解釋](http://stackoverflow.com/questions/12073152/how-to-open-特定版本的字-2007-2010-在-Excel文件)。這不是特定於單詞的,這就是COM/OLE的工作原理。每個COM應用程序都會註冊所有以前的界面,所以當你詢問11.0時,你會得到註冊的應用程序,即Word 2013. –
爲什麼你想要與2003進行互操作呢?您不需要那樣來生成'doc'而不是'docx'文件 - 您可以指定要通過互操作保存的格式。此外,很少有人仍然使用舊格式。 'docx'已經7歲了。你可以在沒有安裝Word的情況下使用OpenXML SDK生成'docx'文件 –