Reputation:
Following code opens two EXCEL.EXE in the Task Manager.
I want to kill second opening EXCEL.EXE from the Task Manager when Form1 is closing.
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Kill all EXCEL.EXE process from Task Manager
For Each prog As Process In Process.GetProcessesByName("EXCEL")
prog.Kill()
Next
Dim FirstxlApp As New Excel.Application 'Open first EXCEL.EXE in the Task Manager
Dim datestart As Date = Date.Now
Dim SecondxlApp As New Excel.Application 'Open second EXCEL.EXE in the Task Manager
Dim dateEnd As Date = Date.Now
SecondxlApp.Visible = True
Dim wb1 As Excel.Workbook
wb1 = SecondxlApp.Workbooks.Open("C:\Book1.xlsx")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim xlp() As Process = Process.GetProcessesByName("EXCEL")
For Each Process As Process In xlp
If Process.StartTime >= datestart And Process.StartTime <= dateEnd Then
Process.Kill()
Exit For
End If
Next
End Sub
End Class
How to declare global variable in order to solve following errors?
Upvotes: 0
Views: 94
Reputation: 659
Your variables dateEnd
and dateStart
are hidden to FormClosing
method as they are only declared within the Form_Load
method.
Change your code to:
Public Class Form1
Dim dateEnd, dateStart As DateTime
Private Sub Form_load
Then they will be accessable to all methods with the form.
Upvotes: 1