Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm Opening Word Document With This Code

VB
Dim missing As Object = System.Reflection.Missing.Value
Dim fileName As Object = "c:\a.doc"
Dim newTemplate As Object = False
Dim docType As Object = 0
Dim isVisible As Object = True
Dim WordApp As New Word.ApplicationClass()
Dim aDoc As Word.Document = WordApp.Documents.Add(fileName, newTemplate, docType, isVisible)
        
WordApp.Visible = False  <------*****
aDoc.Activate()
aDoc.PageSetup.TopMargin = 10
aDoc.PageSetup.BottomMargin = 40
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
WordApp.Selection.Font.Bold = CInt(Word.WdConstants.wdToggle)



I Want to Close Some Word Process .
When i Use This Code To Close Process Of Word I have 1 Problem

VB
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("WINWORD")
For Each p As Process In pProcess
    p.Kill()
Next


If i Opened Word File Manualy
With That Code All Off Word Document Is Closed But I Want To Closed Only Word Process That Create With VbCode In RunTime.
Please Help Me That What Am i Must To Do?

[Modified: First, thank you for using pre tags. But, you have to be careful with them when you paste code in. Sometimes, it recognizes it as code and places the pre tags there for you. So, if you've already put them in, and then paste code, you get two pre tags, which screws up the formatting]

At End Of My Work I Used This
WordApp.Close() or aDoc.Close()
But I Open That Word File With WebBrowser And Want To Close Some Word Process But I Don't Know How Can I Find ?

Please Attention To That
WordApp.Visible = False
And I Can't To Close It Normaly.
I must Close It With Kill Process.
Posted
Updated 8-Jul-10 23:34pm
v4

WordApp.Close() or aDoc.Close() (I believe it's the first one, but you could try both). You shouldn't kill the process. Since you created the application, you also have the handler to it and it has its own close method.
 
Share this answer
 
I think you need the Office SDK for .Net. I don't know anything about it, but with it, you may be able to close single documents (if that's what you meant).

Google is your friend.
 
Share this answer
 
When your application starts the Word application can you get the proccess Id? If so then you can look for the process by name and Id to ensure that you kill the process you want. I'll post some code if I get a little time to mock something up.

Found this...
http://stackoverflow.com/questions/814936/get-pid-from-word-applicationclass[^]
 
Share this answer
 
v3
The problem might be that the COM objects aren't being disposed of. After you call Close() on all of the objects that you're using you also need to call Marshal.ReleaseComObject like so:

WordApp.Close()
Marshal.ReleaseComObject(WordApp)


Some people also say that you have to set the variable to null and then force a garbage collection but in my experiences that's been overkill. That should close the Word instance that you created so that you don't have to enumerate of all Word processes and kill them all. Keep in mind that this goes for all other COM objects created, which includes aDoc and may include other variables that are allocated from WordApp and aDoc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900