13

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.

3
  • 2
    It seems it may be a long standing problem with Word, because it is still there (AFAICT) in 2013. This groups.google.com/forum/?hl=en&fromgroups=#!topic/… suggest using Excel.
    – Fionnuala
    Sep 7, 2012 at 18:55
  • The error looks the same, but both "pale" and "deflate" from that threads works for me. Sep 7, 2012 at 19:45
  • 2
    What I think could be the issue is that all these words have homographs in the list of meanings. For example, with "elite" there is "best (noun)" and "best (adjective)". SynonymInfo interface can not represent both. Word 2007 shows this as two distinctive entries in the Thesaurus panel, but Word 2010 and 2013 Preview show this as combined "best (noun/adjective)" entry. Sep 7, 2012 at 22:04

3 Answers 3

9
+50

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.

0

What's the result if you enter your 'bad' words in MS Word and check for its synonyms? Does it work there? Do you maybe get an additional dialog, which is not handled fine in the API?

1
  • 1
    If you read the second line before Update, it says: "Using the Thesaurus in Microsoft Word manually gives proper results for all words." There are no additional dialogs. Sep 9, 2012 at 9:06
0

I get the same issue for the word "ONE". I have an exception list that catches it...but with 170,000 words, I doubt I'll catch them all :). This is soooo frustrating.

I have a pseudo workaround where I DIMMED "Excludes" as a string and then use this line to catch bad words:

If InStr(Excludes, "[" & AddYourArrayWordHere & "]") Then AddYourArrayWordHere = ""

1
  • I'm also putting an error handler to 'jump' past that word just in case it's not in my exceptions list. May 1, 2021 at 22:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.