Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to demand a running process to open a file?

I know how to launch a file in a new process in C# (for example, open a .doc file in a new Word process window).

But, how to open a file in a "running" process? For example, I already have a running Word process, how to demand it to open a file from a C# program?

I've searched through the Internet and have no idea. :(

like image 664
Phanix Avatar asked Apr 13 '11 09:04

Phanix


2 Answers

You can get active/existing instance of word by using GetActiveObject method from Marshal class.

Word.Application wordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");

Everything else is a same like when you create new word instance.

EDIT:

Word.Application is part of "Microsoft.Office.Interop.Word.dll" so you need to add this reference to your project.

If you dont want to use Early binding (to add a reference), then you can do this via Late binding. You can take a look at this examples: Binding for Office automation servers with Visual C# .NET

like image 74
HABJAN Avatar answered Nov 14 '22 08:11

HABJAN


There is no generic technique that can do this for you. Some applications might allow you to specify an extra parameter from the command line or set an option somewhere.

If you need this functionality for a specific application I suggest you look for an API for that application. Word itself has a Managed wrapper around it's COM API.

like image 3
Jonas Van der Aa Avatar answered Nov 14 '22 07:11

Jonas Van der Aa