Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_SynonymInfo throws insufficient memory exception for some words

I have a simple test program using Microsoft Word Interop to get word meanings from the Thesaurus dictionary:

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)
    }
}

Tried this with Microsoft Office 2010 and Microsoft Office 2013 Preview, using .Net 4.0 in Visual Studio 2010, referencing Office 12 PIA. For over 150 000 different words, this works like a charm. But I notice that for some words, method get_SynonymInfo throws an exception:

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

I know these words are causing an exception:

  • elite (but "elites" works)
  • near
  • tuck
  • tucks
  • walking
  • onboard
  • horrified

This has nothing to do with the exception, but to give a complete source code, I do release memory and quit the Word app properly. There is no msword.exe remaining in the process list when I exit my test program.

        // 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);

Using the Thesaurus in Microsoft Word manually gives proper results for all words.

How can I avoid the exception and get results using Interop code? Is there a valid alternative using some other API?


Update: I was told on a local programming web forum that this code works with Office 2007. And to my surprise, it does. But with Office 2010 and 2013 Preview as stated in this question it does not work, not on my computers/VMs at least. Then I tried to reference Office 14 PIA instead of Office 12, but with Office 2013 Preview this behaves the same.

like image 303
Goran Rakic Avatar asked Sep 07 '12 08:09

Goran Rakic


1 Answers

I can reproduce this problem on my computer on Word 2010. The error code (HResult) is -2146822464 or 0x800A16C0, on which there is nothing to find on the internet.

As Remou notes, this problem existed already back in 2002.

It happens iff the synonym list in Word contains one or more synonyms of two or more types (e.g. adj. / v., n. / v. or adj. / n.). The language/dictionary does not matter. For example, onboard returns in the error in wdEnglishUS but not in wdEnglishUK, since the latter does not know the word. The other words exist in both dictionaries and give an error in either case.

Apparently this is a bug, and only a problem for Word interop, as Word itself shows it all without problems. As far as I could find, this is the only way interop allows you to get synonym information, and there is no work around.

like image 57
Daniel A.A. Pelsmaeker Avatar answered Oct 26 '22 17:10

Daniel A.A. Pelsmaeker