【问题标题】:AutoCorrect Text C# Word自动更正文本 C# Word
【发布时间】:2012-03-07 10:07:54
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net ms-word interop spell-checking


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      1) 使用 Code,自动从 Auto Correct 中选择第一个选项。

      是这样的

      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

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

      【讨论】:

      • 菜单选项已经设置好了,我仍然不断收到对话框,至于第一个选项我不明白,我有兴趣使用 word nit my own 提供的建议。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多