C# 拼写检查问题

发布于 2024-08-31 06:29:56 字数 2892 浏览 2 评论 0原文

我已将拼写检查合并到我的 win forms C# 项目中。这是我的代码。

public void CheckSpelling()
{
    try
    {
        // declare local variables to track error count 
        // and information
        int SpellingErrors = 0;
        string ErrorCountMessage = string.Empty;

        // create an instance of a word application
        Microsoft.Office.Interop.Word.Application WordApp =
            new Microsoft.Office.Interop.Word.Application();

        // hide the MS Word document during the spellcheck
        //WordApp.WindowState = WdWindowState.wdWindowStateMinimize;


        // check for zero length content in text area
        if (this.Text.Length > 0)
        {
            WordApp.Visible = false;

            // create an instance of a word document
            _Document WordDoc = WordApp.Documents.Add(ref emptyItem,
                                              ref emptyItem,
                                              ref emptyItem,
                                              ref oFalse);

            // load the content written into the word doc
            WordDoc.Words.First.InsertBefore(this.Text);

            // collect errors form new temporary document set to contain
            // the content of this control
            Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
            SpellingErrors = docErrors.Count;

            // execute spell check; assumes no custom dictionaries
            WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing);

            // format a string to contain a report of the errors detected
            ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors;

            // return corrected text to control's text area
            object first = 0;
            object last = WordDoc.Characters.Count - 1;
            this.Text = WordDoc.Range(ref first, ref last).Text;
        }
        else
        {
            // if nothing was typed into the control, abort and inform user
            ErrorCountMessage = "Unable to spell check an empty text box.";
        }

        WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);

        // return report on errors corrected
        // - could either display from the control or change this to 
        // - return a string which the caller could use as desired.
       // MessageBox.Show(ErrorCountMessage, "Finished Spelling Check");
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

拼写检查器运行良好,唯一的问题是当我尝试移动拼写检查器时,主窗体由于某种原因变得模糊。此外,当我关闭拼写检查器时,主窗体恢复正常。看起来它正在打开 Microsoft Word,然后隐藏窗口,只允许看到拼写检查器。请帮忙。

I've incorporated spell check into my win forms C# project. This is my code.

public void CheckSpelling()
{
    try
    {
        // declare local variables to track error count 
        // and information
        int SpellingErrors = 0;
        string ErrorCountMessage = string.Empty;

        // create an instance of a word application
        Microsoft.Office.Interop.Word.Application WordApp =
            new Microsoft.Office.Interop.Word.Application();

        // hide the MS Word document during the spellcheck
        //WordApp.WindowState = WdWindowState.wdWindowStateMinimize;


        // check for zero length content in text area
        if (this.Text.Length > 0)
        {
            WordApp.Visible = false;

            // create an instance of a word document
            _Document WordDoc = WordApp.Documents.Add(ref emptyItem,
                                              ref emptyItem,
                                              ref emptyItem,
                                              ref oFalse);

            // load the content written into the word doc
            WordDoc.Words.First.InsertBefore(this.Text);

            // collect errors form new temporary document set to contain
            // the content of this control
            Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
            SpellingErrors = docErrors.Count;

            // execute spell check; assumes no custom dictionaries
            WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing);

            // format a string to contain a report of the errors detected
            ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors;

            // return corrected text to control's text area
            object first = 0;
            object last = WordDoc.Characters.Count - 1;
            this.Text = WordDoc.Range(ref first, ref last).Text;
        }
        else
        {
            // if nothing was typed into the control, abort and inform user
            ErrorCountMessage = "Unable to spell check an empty text box.";
        }

        WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);

        // return report on errors corrected
        // - could either display from the control or change this to 
        // - return a string which the caller could use as desired.
       // MessageBox.Show(ErrorCountMessage, "Finished Spelling Check");
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

The spell checker works well, the only problem is when I try to move the spell checker the main form blurs up for some reason. Also when I close the spell checker the main form is back to normal. It seems like it is opening up Microsoft word then hiding the window, only allowing the spell checker to be seen. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-09-07 06:29:56

我的工作和测试代码片段如下:

string s1 = textBox1.Text;

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

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add();

doc.Words.First.InsertBefore(s1);

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors;

int errorCount = errors.Count;

doc.CheckSpelling(Missing.Value, true, false);

app.Quit(false);

textBox3.Text = errorCount.ToString();

具有错误文本的应用程序。

应用程序错误的文本

窗口将错误的单词显示为红色突出显示的文本。

Word 插件检查单词拼写

错误总数显示在最后。

应用程序显示错误总数

解决方案取自我的 博客

My working and tested code snippet is as follow:

string s1 = textBox1.Text;

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

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add();

doc.Words.First.InsertBefore(s1);

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors;

int errorCount = errors.Count;

doc.CheckSpelling(Missing.Value, true, false);

app.Quit(false);

textBox3.Text = errorCount.ToString();

Application with wrong text.

Application with wrong text

Windows showing wrong word as red highlighted text.

Word plugin checking word spells

Total error count is being showed at the end.

Application showing total number of errors

Solution is taken from my blog.

生生漫 2024-09-07 06:29:56

我尝试使用您的示例代码,但效果不佳,因此我尝试 MSDN 有关该主题的教程

也就是说,我发现这是一个相当老套的解决方案。至于您的主窗体模糊,我想这是因为当您处于拼写检查窗口时它停止响应?您也许可以通过使用新线程来解决它。

另外,你是对的,它正在启动 MS Word,然后隐藏窗口。

就我个人而言,我宁愿使用像 NetSpell 这样的库,而不是依赖 Office 。

I tried using your sample code and it didn't work out as well as it should, so I tried MSDN's tutorial on the subject.

That said, I find it a rather hacky solution. As regards your main form blurring up, I guess it's because it stops responding while you are in the spellchecking window? You might be able to get around it by using a new thread.

Also, you're right, it is launching MS Word, and then hiding the window.

Personally, I'd rather use a library like NetSpell instead of relying on Office.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文