ASP.NET: What is the way to get the process id for Windows Word after creating a Word document using C#?
-
-
Answer:
Well, I've never tried this out personally, but I guess this question from Stack Overflow might be helpful in some way: http://stackoverflow.com/questions/814936/get-pid-from-word-applicationclass
Akshay Kumar at Quora Visit the source
Other answers
Assuming you will be attempting to fetch the Id immediately after creating the Word document, this might work: System.Diagnostics.Process[] wordProcesses = System.Diagnostics.Process.GetProcessesByName("WinWord"); Int32 id = wordProcesses.Last().Id;
Adithya Parthasarathy
I'm curious why you're needing the ID. If it's to shut down Word after you're done with it, I think I have a solution. If what you're doing is anything like the way I used Excel from VB.Net in ASP.Net years ago, you're accessing Word through a managed library, which interfaces to COM. The first thing you need to do is null out all the variables that you can that you used to access the Word document. Then you need to tell Word to "exit" (and then null out any remaining variables that relate to accessing Word, if there are any), and then you need to explicitly run .Net's garbage collector. The reason for this is Word will refuse to exit until all COM objects that are used to access it have a reference count of zero. If even one Office COM object is active, Word will not exit. The .Net interface you're using ups that reference count while its objects are active in memory. Once the garbage collector runs, those objects will be cleared, and then Word will exit. From what I read at the time, explicitly running the garbage collector was "dangerous," because doing so halts your application until the garbage collector finishes, and it's possible the garbage collector may deadlock on some other memory references your application uses, and never return from the call. I tried it out, though, and never ran into that problem. If you don't run the GC directly, the objects will eventually be cleared by the GC's own timing, and Word would probably exit eventually, but if you use Word frequently in this application, that will probably get your application in trouble. So it's worth running the GC explicitly, IMO.
Mark Miller
It depends on how you created the Word document. If you launched the Windows Word process via the System.Diagnostics.Process class, then you can get the process ID via the Id property.
Mike Stall
Related Q & A:
- How to replace a string inside of a Word Document?Best solution by Stack Overflow
- What the best way to get gum off clothes?Best solution by Yahoo! Answers
- How do I attach a word document to an email?Best solution by Yahoo! Answers
- How to put a word document onto an email?Best solution by eHow old
- What is the way to get Jobs in Singapore for Indians?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.