Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kill a specific Winword process

Status
Not open for further replies.

Iamthestig

Programmer
Apr 30, 2008
38
0
0
GB
Hi,

I'm having a problem with moving word documents from one file to another.

Code:
If System.IO.File.Exists("Shared Files\CVs\Original CV\" & objContact.OrigCVFilename) And System.IO.File.Exists("Shared Files\CVs\" & objContact.CVFileName) Then
            System.IO.File.Move("Shared Files\CVs\Original CV\" & objContact.OrigCVFilename, "C:\Program Files\TempCV\TempCV" & objContact.OrigCVFilename)
            System.IO.File.Move("Shared Files\CVs\" & objContact.CVFileName, "Shared Files\CVs\Original CV\" & objContact.CVFileName)
            System.IO.File.Move("C:\Program Files\TempCV\TempCV" & objContact.OrigCVFilename, "Shared Files\CVs\" & objContact.OrigCVFilename)
End If

I'm basically swapping 2 word docs around using a temp folder prevent any read/write conflicts. This works fine the first time it's run, but if I run it again I get a "The process cannot access the file because it is being used by another process." exception.
Now I understand this is because the File.Move creates a Winword process the first time which is still in use the second time it's run. I have played around with killing the process first but all open unrelated word docs will also be killed.
Any idea how I can identify and kill just the File.Move created process?

Thanks for looking.

John
 
Almost certainly you aren't disposing of a variable correctly.

Run FxCop against your source code (both the code you posted and the code which accesses the files), looking for warnings in the 2000 range (anything that has the word "dispose" in it.

If you have any of these, use the "using" keyword to make sure they get disposed of automatically. Or a try..finally block. Or manually calling .Dispose() on them.

Chip H.



____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top