Programatically get path of open Excel/Word-docume

2019-08-08 07:46发布

问题:

Is there any way to get the path of documents currently open in Microsoft Word and Microsoft Excel?

I know how I can fetch the PID and the window handle for the running instance of Excel or Word, but I have no idea how to get the path of the open document. Is it even possible?

I am thinking that an API would be nice, but I doubt it even exists. Perhaps some kind of plugin that sends information to my application is a possibility? (But if any other way is possible, I'd like to avoid having to install plugins everywhere.)

I would prefer to do it using C#, but anything that can be compiled into an executable is alright.

And just in case someone thinks this sounds fishy, it is not to be used for anything remotely illegal. It is for a data collection system at work.

回答1:

There is an API called Office Interop... it is basically a COM based interface for MS Office... and can be used from C# .

For Excel you can get the current file including path via Application.ActiveWorkbook.FullName.

For Word you can get the current file including path via Application.ActiveDocument.FullName.



标签: c# ms-office