SaltyTheFrog
Programmer
VS 2012
I’m looking for a better way to delete an application I started by the PID then this one. This code is pieced together from various posts on Google. It looks for all the WINWORD exes running before I open one putting them in a process list. Then it creates the list again to retrieve the one I just opened by virtue of it not being in the first list. I then use that PID to delete the one I created. Is there a better more succinct way of getting the PID? Something like retrieving it when Word is opened? This code works but it seems like there must be a better way. This one counts on me catching the PID before another version of Word is opened. Getting it returned from opening the application seems better.
I’m doing this for my own knowledge; I realize it is not good practice in an application.
1. Create a new Windows Forms application.
2. Add a button called btnKillByPID
3. Add a reference to Microsoft Word x.x Object Library.
4. Start up a couple of Word applications so something is already out there.
Here is the code:
I’m looking for a better way to delete an application I started by the PID then this one. This code is pieced together from various posts on Google. It looks for all the WINWORD exes running before I open one putting them in a process list. Then it creates the list again to retrieve the one I just opened by virtue of it not being in the first list. I then use that PID to delete the one I created. Is there a better more succinct way of getting the PID? Something like retrieving it when Word is opened? This code works but it seems like there must be a better way. This one counts on me catching the PID before another version of Word is opened. Getting it returned from opening the application seems better.
I’m doing this for my own knowledge; I realize it is not good practice in an application.
1. Create a new Windows Forms application.
2. Add a button called btnKillByPID
3. Add a reference to Microsoft Word x.x Object Library.
4. Start up a couple of Word applications so something is already out there.
Here is the code:
Code:
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub btnKillByPID_Click(sender As Object, e As EventArgs) Handles btnKillByPID.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim PID As Integer
Dim oPara1 As Word.Paragraph
'Get the WINWORD.exe PIDs before opening a new one
Dim pWordInstancesBefore As Process() = Process.GetProcessesByName("WINWORD")
Dim p As System.Diagnostics.Process
'Show what is already running if any
For Each p In pWordInstancesBefore
MessageBox.Show(p.Id)
Next
'Open up Word
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
'Get the WINWORD.exe PIDs before After a new one is created
Dim pWordInstancesAfter As Process() = Process.GetProcessesByName("WINWORD")
'Find the new one by virtue of not being in the first list
For i As Integer = 0 To pWordInstancesAfter.Length - 1
Dim IsFound As Boolean = False
For j As Integer = 0 To pWordInstancesBefore.Length - 1
If pWordInstancesAfter(i).Id = pWordInstancesBefore(j).Id Then
IsFound = True
Exit For
End If
Next
If Not IsFound Then
PID = pWordInstancesAfter(i).Id
Exit For
End If
Next
'ADD a paragraph so I know which document one is mine if there are more than one.
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "This is my document, there are no other documents like it"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()
MessageBox.Show("My PID is: " & PID)
'Kill the process using My PID
Dim aProcess As System.Diagnostics.Process
aProcess = System.Diagnostics.Process.GetProcessById(PID)
aProcess.Kill()
End Sub
End Class