自动更正文本 C# Word

发布于 2025-01-06 05:17:59 字数 1410 浏览 1 评论 0 原文

我正在尝试使用单词自动更正一些非英语文本,问题是当我使用拼写检查功能时,“拼写和语法”对话框会弹出并等待用户输入,我希望文本为自动更正。所以我的问题是我该如何解决这个问题?

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
    }
}

我几天前写了这段代码并且它有效。问题是我卸载了校对工具来运行一些测试,现在我不断收到该对话框,所以我想我可能必须设置一些 Word 设置,或者我在不知情的情况下更改了代码中的某些内容。任何帮助将不胜感激。

我正在使用 Microsoft Office Word 2010

I'm trying to use word to automatically correct some text that is not in English the problem is that when i use the SpellCheck function the "Spell and Grammar" dialog box pop-up and waits for users input and i want the text to be corrected automatically. So my question is how do i solve this ?

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
    }
}

I wrote this code a few days ago and it worked. The problem is that i uninstall proofing tools to run some tests and now i keep getting that dialog so i'm thinking that may i have to set some Word settings or i've changed something in my code without knowing. Any help would be greatly appreciated.

I am using Microsoft Office Word 2010

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

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

发布评论

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

评论(2

七度光 2025-01-13 05:17:59

对于任何可能感兴趣的人来说,这就是我设法解决它的方法,但这确实需要很多时间,因此欢迎任何改进或新想法。

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;
        }
    }

For whoever might be interested this is the way i managed to solve it, but it really takes a lot of time so any improvements or new ideas are welcomed.

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;
        }
    }
未央 2025-01-13 05:17:59

您使用的是哪个 MS Word 版本?

默认情况下,拼写检查器将向您显示该对话框。要禁用该对话框,我知道有两种方法。

1) 使用代码,自动从自动更正中选择第一个选项。

就像这样

AutoCorrect.Entries.Add Name:="AdSAD", Value:="Assad"

2) 或者使用菜单选项。请参考此链接。

主题使用主词典中的单词自动纠正拼写

链接http://office.microsoft.com/en-us/word-help/automatically- Correct-spelling-with-words-from-the-main-dictionary-HA010174790.aspx

如果这不是您想要的,请告诉我?

Which MS Word version are you using?

By default the spell checker will show you the dialog box. To disable the dialog box there are two ways that I know.

1) Using Code, automatically choose the first option from Auto Correct.

It is something like this

AutoCorrect.Entries.Add Name:="AdSAD", Value:="Assad"

2) Or use the menu option. Please refer to this link.

Topic: Automatically correct spelling with words from the main dictionary

Link: http://office.microsoft.com/en-us/word-help/automatically-correct-spelling-with-words-from-the-main-dictionary-HA010174790.aspx

Do let me know if this is not what you want?

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