lundi 16 mars 2015

WinForms C# using MS Word 2010 spell check functionality displays save dialogue

I'm using MS Word's spell checking functionality in my C# WinForms application. This works fine with Office 2007, but for machines with Office 2010 installed I have some trouble. First I had the problem that the word application opened behind my current window and the application would seem frozen. Using alt+tab I can get to the word app. This I fixed by using a workaround by minimizing and the maximizing the word app to show up the spell check dialogue in front of my application's window. Now My last problem is that when I close the spell check window, I still get a 'save' dialogue asking me if I want to save the word document. I just want to close the word app without saving eg. don't want the save dialogue window to display.



Word._Application app = new Word.Application();

//int errors = 0;
if (p_TextToCheck != "")
{
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.Value;
object newTemplate = Missing.Value;
object documentType = Missing.Value;
object visible = false;

Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
doc1.Words.First.InsertBefore(p_TextToCheck);
//Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
//errors = spellErrorsColl.Count;
//doc1.Activate();
//app.ShowMe();
foreach (Word.Window window in app.Windows)
{
window.WindowState = Word.WdWindowState.wdWindowStateMinimize;
window.WindowState = Word.WdWindowState.wdWindowStateMaximize;
}


object optional = Missing.Value;

doc1.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);

//label1.Text = errors + " errors corrected ";
object first = 0;
object last = doc1.Characters.Count - 1;
p_TextToCheck.Value = doc1.Range(ref first, ref last).Text;

object saveChanges = false;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;
doc1.Close(ref saveChanges, ref originalFormat, ref routeDocument);
}

object saveChanges1 = false;
object originalFormat1 = Missing.Value;
object routeDocument1 = Missing.Value;

app.Quit(ref saveChanges1, ref originalFormat1, ref routeDocument1);

Aucun commentaire:

Enregistrer un commentaire