2014-01-09 14 views
1

我開發它採用的Microsoft.Office.Interop.Word參考版本14.安裝在我計算機上的Word版本是一個DLL 2010無法加載的Microsoft.Office.Interop.Word

當我安裝我的DLL上的其他其中有Word 2007或2003版本的機器,我得到以下錯誤:

Could not load file or assembly 'Microsoft.Office.Interop.Word version=14.0.0 

我發現here,設置「嵌入互操作類型」參考「真」可以解決這個問題,但是當我這樣做,錯誤在代碼中使用Microsoft.Office.Interop.Word.ApplicationClass時發生;

Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded 

任何想法我應該怎麼做才能在以前的Word版本的機器上安裝DLL?

+1

http://stackoverflow.com/questions/13383250/could-not-load-assembly-interop-word-version-14-0-0-0?rq=1 – DotNetDeveloper

+0

我已經聯繫我自己 – user3165438

+0

版本'Word'是其中一個原因,爲什麼我使用[後期綁定](http://stackoverflow.com/search?q=c%23+word+late+binding)。 – Sinatr

回答

1

我在這裏給出一個使用後期綁定的例子。它是由前一段時間,也許值得與dynamic被改寫:它

  // try to open word 
      Type typeWordApplication = Type.GetTypeFromProgID("Word.Application"); 
      if (typeWordApplication == null) 
       throw new Exception("Word is not installed (Word.Application is not found)"); 
      object objWordApplication = Activator.CreateInstance(typeWordApplication); 
      object objDocuments = objWordApplication.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, objWordApplication, null); 
      object[] param = new object[] { Missing.Value, Missing.Value, Missing.Value, Missing.Value }; 
      object objDocument = objDocuments.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objDocuments, param); 
      object start = 0; 
      object end = 0; 
      object objRange = objDocument.GetType().InvokeMember("Range", BindingFlags.InvokeMethod, null, objDocument, new object[] { start, end }); 
      // set text 
      objRange.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, objRange, new object[] { text }); 
      // set font 
      object objFont = objRange.GetType().InvokeMember("Font", BindingFlags.GetProperty, null, objRange, null); 
      objFont.GetType().InvokeMember("Name", BindingFlags.SetProperty, null, objFont, new object[] { "Courier" }); 
      objFont.GetType().InvokeMember("Size", BindingFlags.SetProperty, null, objFont, new object[] { (float)8 }); 
      start = objRange.GetType().InvokeMember("End", BindingFlags.GetProperty, null, objRange, null); 
      end = start; 
      objRange = objDocument.GetType().InvokeMember("Range", BindingFlags.InvokeMethod, null, objDocument, new object[] { start, end }); 
      // select text 
      objRange.GetType().InvokeMember("Select", BindingFlags.InvokeMethod, null, objRange, null); 
      objWordApplication.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, objWordApplication, new object[] { true }); 
      Marshal.ReleaseComObject(objWordApplication); 

優點:

  • 你不需要引用或分發任何東西(如果GetTypeFromProgID失敗 - 你剛纔問用戶安裝一些東西)。
  • 它更可能適用於任何已安裝的應用程序版本,因爲許多支持向後兼容性。

缺點:

  • 沒有智能感知,你將不得不瀏覽自己的文件和類對象,搞清楚事情不明顯,獲得經驗等
  • 非常龐大的代碼(除非dynamic使用) ;
+0

非常感謝您的快速回復!我現在來看看它。 – user3165438

+0

我試過上面的代碼 - 它創建了另一個文檔,除了我之前的文檔。那是什麼意思? – user3165438

+0

這是因爲它調用'Documents.Add'。我不知道什麼是「你之前擁有的」文件。如果您有代碼,那麼如果您決定使用後期綁定,則必須將其轉換,如上所述。 – Sinatr