我有一个内部Windows窗体应用程序,我想使用拼写检查。每个人都安装了Office 2007,所以我不应该有问题,但我无法让它完全工作。
这就是我所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
namespace Refraction.Spelling
{
public static class SpellCheckers
{
public static string CheckSpelling(string text)
{
Word.Application app = new Word.Application();
object nullobj = Missing.Value;
object template = Missing.Value;
object newTemplate = Missing.Value;
object documentType = Missing.Value;
object visible = true;
object optional = Missing.Value;
object savechanges = false;
Word._Document doc = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
doc.Words.First.InsertBefore(text);
Word.ProofreadingErrors errors = doc.SpellingErrors;
var ecount = errors.Count;
doc.CheckSpelling(ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional, ref optional, ref optional,
ref optional, ref optional, ref optional);
object first = 0;
object last = doc.Characters.Count - 1;
var results = doc.Range(ref first, ref last).Text;
doc.Close(ref savechanges, ref nullobj, ref nullobj);
app.Quit(ref savechanges, ref nullobj, ref nullobj);
return results;
}
}
}
我这样使用它:
memDirectionsToAddress.Text = SpellCheckers.CheckSpelling(memDirectionsToAddress.Text);
现在,这成功弹出Word中的拼写检查对话框并检测到任何拼写错误的单词但我无法在WinForm应用程序中进行更正。
此外,它将Word Doc的“Shell”保留为已更正的文本。我怎么不显示或至少让它消失?
两件事:
由于
答案 0 :(得分:1)
接下来的步骤是:
更多信息:
答案 1 :(得分:0)
我有一个旧脚本,其中所有功能都被调用,因此单词本身不需要DLL引用:
internal class SpellChecker
{
public SpellChecker()
{
}
public static string Check(string text)
{
bool flag;
string str = text;
flag = (text == null ? true : !(text != ""));
bool flag1 = flag;
if (!flag1)
{
Type typeFromProgID = Type.GetTypeFromProgID("Word.Application");
object obj = Activator.CreateInstance(typeFromProgID);
object[] objArray = new object[1];
object obj1 = typeFromProgID.InvokeMember("Documents", BindingFlags.GetProperty, null, obj, null);
object obj2 = obj1.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, obj1, null);
object obj3 = obj2.GetType().InvokeMember("ActiveWindow", BindingFlags.GetProperty, null, obj2, null);
objArray[0] = 0;
obj3.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, null, obj3, objArray);
object[] objArray1 = new object[] { -2000, -2000 };
obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1);
objArray[0] = "Spell Check";
obj3.GetType().InvokeMember("Caption", BindingFlags.SetProperty, null, obj3, objArray);
object obj4 = typeFromProgID.InvokeMember("Selection", BindingFlags.GetProperty, null, obj, null);
objArray[0] = text;
obj4.GetType().InvokeMember("TypeText", BindingFlags.InvokeMethod, null, obj4, objArray);
objArray[0] = 6;
obj4.GetType().InvokeMember("HomeKey", BindingFlags.InvokeMethod, null, obj4, objArray);
object obj5 = obj2.GetType().InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, obj2, null);
int num = (int)obj5.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, obj5, null);
flag1 = num <= 0;
if (flag1)
{
System.Windows.Forms.MessageBox.Show("Spellcheck is correct");
}
else
{
obj3.GetType().InvokeMember("Activate", BindingFlags.InvokeMethod, null, obj3, null);
objArray1 = new object[] { -5000, -5000 };
obj.GetType().InvokeMember("Move", BindingFlags.InvokeMethod, null, obj, objArray1);
objArray[0] = true;
obj.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, obj, objArray);
obj2.GetType().InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, obj2, null);
objArray[0] = true;
obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray);
object obj6 = obj2.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, obj2, null);
str = obj6.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, obj6, null).ToString();
str = str.Trim();
}
flag1 = obj == null;
if (!flag1)
{
objArray[0] = true;
obj2.GetType().InvokeMember("Saved", BindingFlags.SetProperty, null, obj2, objArray);
obj.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, obj, null);
}
}
string str1 = str;
return str1;
}
}
只需将文字提供给它,它将返回您批准的任何更正。