9

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. :(

tshepang
  • 10,772
  • 21
  • 84
  • 127
Phanix
  • 91
  • 2
  • I think you could use DDE somehow. I think that's the way explorer sends messages to running ups to tell them to open some file. – CodesInChaos Apr 13 '11 at 09:31

4 Answers4

5

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

HABJAN
  • 8,803
  • 3
  • 32
  • 54
  • Hi, Habjan, I think that's for creating a new word process, but how to ask a "running" word process to open a file? – Phanix Apr 13 '11 at 09:17
  • This is a very specific example that creates dependencies on Word. – Jonas Van der Aa Apr 13 '11 at 09:17
  • @Phanix: GetActiveObject will get you running instance. Description says: obtains a running instance of the specified object from the running object table (ROT). – HABJAN Apr 13 '11 at 09:20
  • @Jonas Van der Aa: what do you mean by saying "creates dependencies" ? – HABJAN Apr 13 '11 at 09:22
  • @Jonas: that would be rather moot point. The goal was to make Word open a document. Wouldn't that be a rather stringent dependency on Word to begin with :) – sehe Apr 13 '11 at 09:23
  • @HABJAN If I'm not mistaken `Word.Application` is from the managed wrapper for Word's COM API? I'm not saying it's a problem but if that is the case you might want to mention that he needs to add the dependencies to his project :) – Jonas Van der Aa Apr 13 '11 at 09:24
  • @sehe: He stated Word as an example but I assumed he wanted a generic solution – Jonas Van der Aa Apr 13 '11 at 09:25
  • @Jonas Van der Aa: yep, you are right. I added that part to my answer. – HABJAN Apr 13 '11 at 09:27
  • @Jonas Van der Aa: even, it's not required if he use Late Binding. – HABJAN Apr 13 '11 at 09:28
3

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.

Jonas Van der Aa
  • 1,392
  • 10
  • 26
  • This is correct. Every app does it differently. Look up DDE and start digging into the registry for different file types to see what the commands normally look like. –  Apr 13 '11 at 09:47
0

ShellExecute equivalent in .NET

ShellExecute is what you want.

ShellExecute is the Win32 API that has the desired behaviour. It is just hidden behind ProcessStartInfo.UseShellExecute for .NET.

Or outside SO:

http://www.vbforums.com/archive/index.php/t-257526.html

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565
  • 1
    Not to nitpick but don't you mean "Process.Start is what you want"? – Jonas Van der Aa Apr 13 '11 at 09:31
  • Not really. ShellExecute is the Win32 API that has the desired behaviour. It is just hidden behind `ProcessStartInfo.UseShellExecute` for .NET; I explicitely refer to it as ShellExecute, _just so I won't go unnoticed_ – sehe Apr 13 '11 at 09:40
  • @sehe Yep, just like that: ShellExecute bla-bla-bla. ShellExecute is what you want. ShellExecute rubble-rubble-rubble... Did i mention *ShellExecute*? – ivan_pozdeev Aug 27 '14 at 14:32
0

If you use Process.Start("mydoc.doc"), then if there is an existing Word process, it will be reused. According to MSDN:

If the process is already running, no additional process is started. Instead, the existing process resource is reused and no new Process component is created

mcanti
  • 1,884
  • 2
  • 16
  • 14