Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. How to kill excel.exe for that particular opened excel in vb.net (window application)

How to kill excel.exe for that particular opened excel in vb.net (window application)

Scheduled Pinned Locked Moved Windows Forms
questioncsharphelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eunice VB junior
    wrote on last edited by
    #1

    Hi, I try to export datagrid data to one excel spread sheet (i called it ExcelA). Then, when I want to close ExcelA, I wrote this codes to kill excel.exe. wExcel.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(wExcel) System.GC.Collect() Dim proc As System.Diagnostics.Process For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") proc.Kill() Next wExcel = Nothing But then, it killed all the excel that I currently open. (those that are not related to this ExcelA.) How can i only kill the excel.exe that belonging to this ExcelA ? Please help. I had tried to search.. but failed to get it. Thanks in advanced. Appreciate your help. :) Here is my codes: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds.Clear() ds.Tables.Add() If ds.Tables(0).Columns.Count <= 0 Then For i As Integer = 0 To myTable.Columns.Count - 1 ds.Tables(0).Columns.Add(myTable.Columns(i).ColumnName) Next End If Dim dr1 As DataRow For k As Integer = 0 To myTable.Rows.Count - 1 dr1 = ds.Tables(0).NewRow For j As Integer = 0 To myTable.Columns.Count - 1 dr1(j) = dgSearch.Item(k, j) Next ds.Tables(0).Rows.Add(dr1) Next Dim wExcel As New Excel.ApplicationClass Dim wBook As Excel.Workbook Dim wSheet As Excel.Worksheet wBook = wExcel.Workbooks.Add() wSheet = wBook.ActiveSheet() Dim dt As System.Data.DataTable = ds.Tables(0) Dim dc As System.Data.DataColumn Dim dr As System.Data.DataRow Dim colIndex As Integer = 0 Dim rowIndex As Integer = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(1, colIndex) = dc.ColumnName Next For Each dr In dt.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName) Next Next wSheet.Columns.AutoFit() Dim strFileName As String = "C:\M2.xls" Dim blnFileOpen As Boolean = False Try Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName) fileTemp.Close() Catch ex As Exception

    P 1 Reply Last reply
    0
    • E Eunice VB junior

      Hi, I try to export datagrid data to one excel spread sheet (i called it ExcelA). Then, when I want to close ExcelA, I wrote this codes to kill excel.exe. wExcel.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(wExcel) System.GC.Collect() Dim proc As System.Diagnostics.Process For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") proc.Kill() Next wExcel = Nothing But then, it killed all the excel that I currently open. (those that are not related to this ExcelA.) How can i only kill the excel.exe that belonging to this ExcelA ? Please help. I had tried to search.. but failed to get it. Thanks in advanced. Appreciate your help. :) Here is my codes: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds.Clear() ds.Tables.Add() If ds.Tables(0).Columns.Count <= 0 Then For i As Integer = 0 To myTable.Columns.Count - 1 ds.Tables(0).Columns.Add(myTable.Columns(i).ColumnName) Next End If Dim dr1 As DataRow For k As Integer = 0 To myTable.Rows.Count - 1 dr1 = ds.Tables(0).NewRow For j As Integer = 0 To myTable.Columns.Count - 1 dr1(j) = dgSearch.Item(k, j) Next ds.Tables(0).Rows.Add(dr1) Next Dim wExcel As New Excel.ApplicationClass Dim wBook As Excel.Workbook Dim wSheet As Excel.Worksheet wBook = wExcel.Workbooks.Add() wSheet = wBook.ActiveSheet() Dim dt As System.Data.DataTable = ds.Tables(0) Dim dc As System.Data.DataColumn Dim dr As System.Data.DataRow Dim colIndex As Integer = 0 Dim rowIndex As Integer = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(1, colIndex) = dc.ColumnName Next For Each dr In dt.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName) Next Next wSheet.Columns.AutoFit() Dim strFileName As String = "C:\M2.xls" Dim blnFileOpen As Boolean = False Try Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName) fileTemp.Close() Catch ex As Exception

      P Offline
      P Offline
      Priyavp
      wrote on last edited by
      #2

      Try using environment.exit(exitcode) in the workbook close event.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups