Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to use the spellCheck feature provided by word to correct a string that is not in English, the problem is that when i use the spellCheck function i get the Spelling and Grammar dialog which requires user input and i don't want that to happen.
This is my code
C#
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using TobyCL.ro.toby.StringOperations;
namespace namespace.ro.toby
{
    class WordProofing:IProof
    {
        private readonly Word.Application _wordApp;
        private readonly Word.Document _wordDoc;
        private static object _oEndOfDoc = "\\endofdoc";
        public WordProofing()
        {

            _wordApp = new Word.Application {Visible = false};
            _wordDoc = _wordApp.Documents.Add();
        }
        public void Close()
        {
            object obj = Word.WdSaveOptions.wdDoNotSaveChanges;
            _wordDoc.Close(ref obj);
            _wordApp.Quit(ref obj);
        }
        #region Implementation of IProof

        public string Proof(string proofText)
        {
            Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range;
            wRng.Text = proofText;
            _wordDoc.CheckSpelling(IgnoreUppercase: true,AlwaysSuggest:false);
            string str = wRng.Text;
            wRng.Text = "";
            return str;
        }
        #endregion
    }
}


It worked last week when i wrote it, but in the meantime i uninstalled Office proofing tools, and reinstalled it. So i'm thinking that maybe the a setting i have to set or maybe i accidentally change something in my code (but i doubt the last one).

Thanks in adavance
Posted

1 solution

I've solved it but it's really really slow, so any new ideas are welcomed.
C#
using Microsoft.Office.Interop.Word;
    class WordProofing
    {
        private Application _wordApp;
        private readonly Document _wordDoc;
        private static object _oEndOfDoc = "\\endofdoc";
        public WordProofing()
        {

            _wordApp = new Application { Visible = false };
            _wordDoc = _wordApp.Documents.Add();
        }
        public void Close()
        {
            _wordDoc.Close(WdSaveOptions.wdDoNotSaveChanges);
            _wordApp.Quit();
        }

        public string Proof(string proofText)
        {
            Range wRng = _wordDoc.Bookmarks.get_Item(ref _oEndOfDoc).Range;
            wRng.Text = proofText;
            ProofreadingErrors spellingErros = wRng.SpellingErrors;
            foreach (Range spellingError in spellingErros)
            {
                SpellingSuggestions spellingSuggestions =
                                                            _wordApp.GetSpellingSuggestions(spellingError.Text,IgnoreUppercase:true);

                foreach (SpellingSuggestion spellingSuggestion in spellingSuggestions)
                {
                    spellingError.Text = spellingSuggestion.Name;
                    break;
                }
            }

            string str = wRng.Text;
            wRng.Text = "";
            return str;
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900