I'm deploying an Outlook 2007 add-in being created in Visual Studio 2008 using C#. I've created a function to extract text from a word document.
The problem is that about 20% of the time, this will temporarily grab focus and give it to my addin. I sat and watched sysinternal's process explorer for about 10 minutes while my add-in was running and it only happened when the word process was running for more than like 2 seconds. After the WINWORD proceess closes - focus will revert back to whatever program had it previously.
Does anybody know why the WINWORD process would steal focus on behalf of my add-in and how to avoid it?
public static string ExtractWordDocument(object filename)
{
Word.Application wordApp = new Word.Application();
string content = null;
Word.Document doc = null;
try
{
doc = wordApp.Documents.OpenNoRepairDialog(
ref filename,
ref officeObjects.Negative,
ref officeObjects.ReadOnly,
ref officeObjects.Negative,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Negative,
ref officeObjects.Negative,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj);
content = doc.Content.Text;
}
catch (Exception exc)
{
Util.Log("Error processing word document - Exception received: " + exc.ToString());
}
if (doc != null)
{
((Word._Document)doc).Close(ref officeObjects.SaveChanges, ref officeObjects.OriginalFormat, ref officeObjects.RouteDocument);
}
((Word._Application)wordApp).Quit(ref officeObjects.SaveChanges, ref officeObjects.OriginalFormat, ref officeObjects.RouteDocument);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp);
doc = null;
wordApp = null;
return content;
}