我有使用Microsoft Word互操作的一個簡單的測試程序,從同義詞辭典獲得詞義內存異常不足:get_SynonymInfo拋出了一些話
using System;
using Word = Microsoft.Office.Interop.Word;
class Program
{
static void Main(string[] args)
{
string word = "elite"; // = "common";
var app = new Word.Application();
var synInfo = app.SynonymInfo[word, Word.WdLanguageID.wdEnglishUS];
if (synInfo.Found && synInfo.MeaningCount > 0)
{
foreach (var meaning in synInfo.MeaningList as Array)
Console.WriteLine(meaning.ToString());
}
// release memory and quit Word app... (see below)
}
}
與Microsoft Office 2010和Microsoft Office 2013預覽試過,用.NET 4.0在Visual Studio 2010中,引用Office 12 PIA。對於超過15萬個不同的單詞,這就像一個魅力。但我注意到,一段話,法get_SynonymInfo拋出一個異常:
Unhandled Exception: System.Runtime.InteropServices.COMException: Insufficient memory or disk space.
at Microsoft.Office.Interop.Word.ApplicationClass.get_SynonymInfo(String Word, Object& LanguageID)
at WordInteropTest.Program.Main(String[] args) in Program.cs:line 11
我知道這些話是引起異常:
- 精英(但「精英」的作品)
- 附近
- 掖
- 塔克斯
- 走
- onboar d
- 嚇壞
這有什麼好做的例外,但給一個完整的源代碼,我不釋放內存和正確退出的Word應用程序。當我退出測試程序時,進程列表中沒有剩餘msword.exe。
// release memory and quit Word app... (continuing from above)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(synInfo);
app.Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
在Microsoft Word中使用同義詞庫手動爲所有單詞提供正確的結果。
如何避免異常並使用Interop代碼獲得結果?有沒有使用其他API的有效替代方案?
更新:我在local programming web forum告訴記者,該代碼可以使用Office 2007的讓我吃驚,它的作用。但是在這個問題中提到的Office 2010和2013 Preview不起作用,至少在我的電腦/虛擬機上不行。然後我嘗試引用Office 14 PIA而不是Office 12,但對於Office 2013 Preview,其行爲相同。
它似乎可能是Word的一個長期存在的問題,因爲它在2013年仍然存在(AFAICT)。此https://groups.google.com/forum/?hl=zh-CN&fromgroups=#!topic/microsoft。 public.office.developer.automation/ex58NtYsJmc建議使用Excel。 – Fionnuala
錯誤看起來是一樣的,但是來自該線程的「蒼白」和「放氣」都適用於我。 –
我認爲可能的問題是所有這些單詞在意義列表中都有同形異義詞。例如,「精英」有「最好(名詞)」和「最佳(形容詞)」。 SynonymInfo接口不能代表兩者。 Word 2007將此顯示爲「同義詞庫」面板中的兩個不同的條目,但Word 2010和2013 Preview將此顯示爲組合的「最佳(名詞/形容詞)」條目。 –