Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Here is a code for a button on form1:

 

Dim Xl As New Excel.Application()

Dim Xlb As Excel.Workbook = Xl.Workbooks.Open("C:\Kk.xls")

Dim Xls As Excel.Worksheet = CType(Xlb.Worksheets(1), Excel.Worksheet)

TextBox1.Text = Xls.Cells(1, 1).value

Xls.Cells(1, 1).Value = "Something new"

Xlb.Save()

Xlb.Close()

Xl.Workbooks.Close()

Xl.Quit()

 

When I click that, everything works as planned, but EXCEL.EXE is still a running process. The result is that you can not open Kk.xls through excel until my program is ended entirely. The problem with this is if my program crashes or ends by any other method other than the END command, Excel is still running and keeping that file in use. How do I completely shut down Excel after running the above code?

  • 1 month later...
Posted

I know this might be a bit of overkill but i got this to work in .net with it opening the excel file writing the value "Something New" and then closing it ( and destorying the Excel process in the task manager)

 

Hope this works for you

 

Dim Xl As New Excel.Application()

Dim Xlb As Excel.Workbook = Xl.Workbooks.Open("C:\Kk.xls")

Dim Xls As Excel.Worksheet = CType(Xlb.Worksheets(1), Excel.Worksheet)

TextBox1.Text = Xls.Cells(1, 1).value

Xls.Cells(1, 1).Value = "Something new"

Xlb.Save()

 

'Changed Code

 

exWorkbook.Close()

exObj.Workbooks.Close()

exObj.Application.Quit()

exObj.Quit()

 

exWorksheet = Nothing

exWorkbook = Nothing

exObj = Nothing

 

GC.Collect()

GC.WaitForPendingFinalizers()

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...