使用C#自动更正Word中的文本 [英] Autocorrect text in Word with c#
问题描述
我正在尝试使用单词提供的spellCheck功能来更正非英语字符串,问题是当我使用spellCheck功能时,我得到了需要用户输入的拼写和语法"对话框,而我没有不想发生这种情况.
这是我的代码
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
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
}
}
上周我写它时,它起作用了,但是与此同时,我卸载了Office校对工具,然后重新安装了它.所以我在想,这可能是我必须设置的设置,或者是我不小心更改了代码中的某些内容(但我怀疑最后一个).
非常感谢
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
推荐答案
我已经解决了它,但是它确实很慢,所以欢迎任何新想法.
I''ve solved it but it''s really really slow, so any 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;
}
}
这篇关于使用C#自动更正Word中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!