110,538
社区成员
发帖
与我相关
我的任务
分享
using Word = Microsoft.Office.Interop.Word;
public static bool SourceStringSpellingCheck(LocResource lr,ref string message)
{
try
{
string[] wordList = lr.SourceString.Value.Split(LCXOMToolbox.charArray, StringSplitOptions.RemoveEmptyEntries);
object missing = System.Reflection.Missing.Value;
Word._Application app = null;
try
{
app = new Word.Application();
}
catch
{
Console.WriteLine("The Word Application can not be initialized, make sure you have MS office Word installed");
return false;
}
int errors = 0;
bool result = false;
app.Visible = false;
// Setting these variables is comparable to passing null to the function.
// This is necessary because the C# null cannot be passed by reference.
object template = missing;
object newTemplate = missing;
object documentType = missing;
object visible = true;
//Setting these variables is comparable to passing null to the function.
//Close and Quit method.
object saveChanges = false;
object originalFormat = missing;
object routeDocument = missing;
foreach (string word in wordList)
{
if (word.Length > 1)
{
Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
doc1.Words.First.InsertBefore(word);
Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
errors = spellErrorsColl.Count;
if (errors > 0)
{
message = string.Format("{0}{1};", message, word);
errors = 0;
result = true;
}
//Close document
doc1.Close(ref saveChanges, ref originalFormat, ref routeDocument);
}
}
//Quit application
app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
return result;
}
catch (Exception ex)
{
Console.WriteLine("Some exception occurs:{0}", ex.Message);
return false;
}
}