Reputation: 13972
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;
}
Upvotes: 1
Views: 699
Reputation: 13972
No reason for this behavior was ever found but we ended up going with an IFilter implementation which made it pointless.
For anybody else looking at simply extracting text from a word document - don't use word, use an IFilter.
Upvotes: 1
Reputation: 23796
What if you say:
wordApp .Visible = false;
?
Edit: Oh, wait you're saying your addin will get focus, not the word instance. Oh well, try the above anyway :)
Upvotes: 0