How to add a specific word to the Word dictionary?
  • What do you mean by that? - Alexander Alexandrovich
  • I have a string variable. It contains some word. This word should be in the MS Word dictionary. - Janycz

1 answer 1

using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading.Tasks; using Word = Microsoft.Office.Interop.Word; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Word._Application word = new Word.Application(); word.Documents.Add(); string dictfilename=word.CustomDictionaries.ActiveCustomDictionary.Path+"\\"+word.CustomDictionaries.ActiveCustomDictionary.Name; FileStream fs; try { fs = new FileStream(dictfilename, FileMode.OpenOrCreate, FileAccess.ReadWrite); Console.WriteLine("Открыт файл " + dictfilename); Console.ReadKey(); } catch { Console.WriteLine("Не могу открыть файл словаря " + dictfilename); Console.ReadKey(); return; } StreamWriter sw = new StreamWriter(fs,Encoding.UTF8); sw.WriteLine("rihgths"); sw.Close(); word.Quit(); } } } 
  • one
    The disadvantage of this approach is that the installed Word on the machine is required for this code to work. I would advise to look in the direction of OpenXml, because it is more reliable. - mrakolice
  • Word User Dictionary is a text file encoded in UTF-8. What can OpenXML do to work with it? Word itself is used solely to get the path to the user dictionary (in my example by default), and I don’t know how to do it without Word installed, to be honest. No, of course you can make word creation in try, and in catch you can ask to enter the name of the dictionary file ... but something tells me that the person had a question in the other :) - Zufir
  • > A custom Word dictionary is a text file encoded in UTF-8. @Zufir whatever it is, your solution implies that MS Word must be installed on the machine. Using Open XML does not require this. Moreover, there are doubts that your solution will work for Word, starting from version 2007 - DreamChild
  • @DreamChild, I have Word 2010 installed, the solution works. Perhaps it will not work with Office 2013, in which the spell-cheating module has changed. Tomorrow I’ll check at work, I’m there at 2013. Than working with a text file that is not an Office document will help OpenXML - I still don’t understand. Office objects are not used to write to a file — they are used only to locate this file. But yes. You can put out the receipt of the dictionary in one method, and the record - in another, and overload the write method regarding the type of dictionary. True, then the word Word in question will be superfluous - Zufir