Microsoft Office Interop Word Application Online Rating: 9,7/10 7183 votes

Dotnet add package Microsoft.Office.Interop.Word --version 15.0.4797.1003 paket add Microsoft.Office.Interop.Word --version 15.0.4797.1003 The NuGet Team. Class Library Microsoft.Office.Interop.Word Global Interface. Global Interface Global Members. Global Members. Sets the folder in which Microsoft Word searches for documents. (Inherited from _Global.). Returns an Options object that represents application settings in Microsoft Word. (Inherited from _Global.).

Active2 years, 7 months ago

I'm creating a new instance of Word using the Office interop by doing this:

I can get a window handle like this:

The problem is that code works on the assumption that there's no other instance of Word running. If there are multiple, it can't guarantee that the handle it returns is for the instance that I've launched. I've tried using GetForegroundWindow after detecting a WindowActivate event from my object but this is all running within a WPF application that's set to run as the topmost window, so I just get the handle to the WPF window. Are there any other ways to get the handle for my instance of word?

HotNHotN
2,8372 gold badges27 silver badges44 bronze badges

5 Answers

Not sure why you need the handle to Word, but one way I've done this before is to actually change the Word window caption and search for it. I did this because I wanted to host the Word application inside a control, but that's another story. :)

Once you got the handle, you can restore the caption if you like.

Eddie PazEddie Paz

You are already getting a list of all Word processes. You can iterate through this list, get the parent ID of each process and match is against the current process i.e. your own application that created a Word instance. This is roughly what I have in mind:

Karle baby dance song free download songs. The ProcessExtensions class is available in this excellent response to an earlier post. I have used this class in my own code and have had no complaints.

Community
AbbasAbbas
5,7573 gold badges29 silver badges48 bronze badges

I'll leave the answer I've selected as correct, since it was what I found to work back when I wrote this. I've since needed to do do something similar for a different project and found that trying to update the application caption seemed to be less reliable (Office 2013 vs. 2010? Who knows..). Here's the new solution I've come up with that leaves window captions intact.

This uses the following custom comparer to ensure the processes match (found here).

Community
HotNHotN
2,8372 gold badges27 silver badges44 bronze badges

This answer explains how to get the Word.Application object from a hwnd, which means we can loop through all active Word processes and check if their Word.Application matches our own Word.Application object. This way, you don't need to do anything with the window caption.

Microsoft Office Interop Excel

Please note that you can only obtain the process of a Word.Application that is visible and has one or more documents open (the code opens a temporary empty document in the latter case):

I use NetOffice in this example, but you can easily alter it to work with the standard interop libraries by editing the using statement and doing Marshal.ReleaseComObject() instead of Word.Application.Dispose().

Microsoft Office Interop Word Example

Community
JbjstamJbjstam

Another method, making use of the fact that injected macros are run directly inside the WINWORD process:

JbjstamJbjstam

Not the answer you're looking for? Browse other questions tagged .netoffice-interopwindow-handles or ask your own question.

Coments are closed
Scroll to top