I'm using MSWord interop to check grammar/spell in my application. I'm using these steps to do this:
1) Create a new Single Thread Apartment to not lock my application's form
2) Disable the input of my application
3) Using reflection (to be MSOffice version independent), I'm using this code to open Word:
objWord = System.Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application")); Object objDocuments = objWord.GetType().InvokeMember("Documents", BindingFlags.GetProperty, null, objWord, null); objDoc = objDocuments.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, objDocuments, null); objContent = objDoc.GetType().InvokeMember("Content", BindingFlags.GetProperty, null, objDoc, null); IDataObject oldObjData = Clipboard.GetDataObject(); Clipboard.SetDataObject(text); objContent.GetType().InvokeMember("Paste", BindingFlags.InvokeMethod, null, objContent, null); objDoc.GetType().GetMethod("CheckGrammar").Invoke(objDoc, null); objWord.GetType().GetProperty("Visible").SetValue(objWord, false, null); objContent.GetType().InvokeMember("Cut", BindingFlags.InvokeMethod, null, objContent, null); IDataObject objData = Clipboard.GetDataObject(); objDoc.GetType().GetProperty("Saved").SetValue(objDoc, true, null); objDoc.GetType().GetMethod("Close").Invoke(objDoc, new Object[] { null, null, null }); objWord.GetType().GetMethod("Quit").Invoke(objWord, new Object[] { null, null, null });
But when I call this, only in Windows Vista, the SpellCheck window opens in the back of my application, and I need to use ALT+TAB to show the Word's window.
Anybody had this problem or have a suggestion how to solve?
I tried to call
objDoc.GetType().GetMethod("Activate").Invoke(objDoc, null);
but it doesn't work. Other "Focus" methods neither.
Thanks