Microsoft.Office.Interop.Word拼写检查失败

时间:2019-11-19 15:28:24

标签: c#

我试图让CheckSpelling告诉我一个单词文档是否已进行拼写检查,但是无论我做什么,它都会返回对每个单词的拼写检查均失败的结果,即使它们正确无误。谁能告诉我我要去哪里错了?这是我的代码:

var allwords = aDoc.Range(aDoc.Content.Start, aDoc.Content.End).Text.Split(' '); 
                    foreach (var writtenword in allwords){ 
                        if (! wordApp.CheckSpelling(writtenword))
                        {
                        wordApp.GetSpellingSuggestions(writtenword); 
                                }
                    }
                    wordApp = new Word.Application();
                    var docpath = "C:\\Users\\netha\\Documents\\FSharpTest\\FTEST\\ftestdoc3.docx";
                    wordApp.Documents.Add(docpath);
                    aDoc = wordApp.Documents[1];
                    wordApp.CheckSpelling(aDoc.Range(aDoc.Content.Start, aDoc.Content.End).Text);
                    foreach(var writtenword in aDoc.Range(aDoc.Content.Start, aDoc.Content.End).Text.Split(' ') ) {
                        if (!wordApp.CheckSpelling(
                                writtenword,
                                ref CustomDictionary,
                                ref IgnoreUppercase,
                                ref MainDictionary,
                                ref CustomDictionary2,
                                ref CustomDictionary3,
                                ref CustomDictionary4,
                                ref CustomDictionary5,
                                ref CustomDictionary6,
                                ref CustomDictionary7,
                                ref CustomDictionary8,
                                ref CustomDictionary9,
                                ref CustomDictionary10)) {
                        Console.WriteLine("Spellcheck failed on " + writtenword);
                        wordApp.GetSpellingSuggestions(
                            writtenword,
                            ref CustomDictionary,
                            ref IgnoreUppercase,
                            ref MainDictionary,
                            ref CustomDictionary2,
                            ref CustomDictionary3,
                            ref CustomDictionary4,
                            ref CustomDictionary5,
                            ref CustomDictionary6,
                            ref CustomDictionary7,
                            ref CustomDictionary8,
                            ref CustomDictionary9,
                            ref CustomDictionary10); }
                    }

                    allwords = aDoc.Range(aDoc.Content.Start, aDoc.Content.End).Text.Split(' '); 
                    foreach (var writtenword in allwords)
                { 
                    if (!wordApp.CheckSpelling(writtenword)) {
                        wordApp.GetSpellingSuggestions(writtenword);

                } 
                }

1 个答案:

答案 0 :(得分:1)

此代码从文档中检索包含拼写错误的单词。如果您得到意想不到的结果,则可能意味着它不是您要使用的校对语言。

var app = new Microsoft.Office.Interop.Word.Application();

try
{
    Document doc = app.Documents.Open(pathToFile);

    foreach (var word in doc.Words.Cast<Range>())
    {
        if (word.SpellingErrors.Count > 0)
        {
            Console.WriteLine(word.Text);
        }
    }
}
catch
{
    //Use Try/Catch to avoid persisting Word processes in the event of an exception
}
finally
{
    app.Quit();
}