Reputation: 20107
I'm new to office interop, but I can't for the life of me work out how to get an instance of the Application or Document class that relates to the the word application that is currently open (ie not create a new one). Is this possible?
Upvotes: 3
Views: 743
Reputation: 3230
It has been a while..maybe there is now a cleaner way without having to use Visual Basic from C Sharp; Using C Sharp with Office Interop has improved recently. I dug this up from very old code, but I use it a lot:
using Microsoft.VisualBasic;
Application wordApp = (Microsoft.Office.Interop.Word.Application)Interaction.GetObject(null, "Word.Application");
Note the use of null for first parameter PathName
. Use of empty string would return a new object instance of Word Application. If you omit the PathName
, GetObject
will return currently active object.
You may want to wrap in a try/catch
, and if COM exception occurs, use CreateObject
(unless these new-fangled programming practices call this bad practice)
Upvotes: 3