Importing from Excel using Visual Basic - excel

So here's the deal. I want to import some information from my Excel file to "play around" with them in VBA. The problem is that there is a certain method of import, which I kinda want to use for some Mapping that will simply not work for me.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim oApp As New MapPoint.Application
oApp.Visible = True
oApp.UserControl = True
Dim oMap As MapPoint.Map
oMap = oApp.NewMap()
oMap.MapStyle = GeoMapStyle.geoMapStyleData
Dim wtf As String
wtf = "C:\users\admin\Desktop\Clienti.xlsx!Sheet1!B1:C8"
Dim ods As MapPoint.DataSet
ods = oApp.ActiveMap.DataSets.ImportData(wtf, , GeoCountry.geoCountryRomania, GeoDelimiter.geoDelimiterDefault, 0)
ods.Symbol = 6
ods.ZoomTo()
MsgBox("Closing MapPoint")
Try
oApp.Quit()
Catch ex As Exception
MsgBox("Already closed")
End Try
End Sub
After I use this, I get the following error
I also have Imports MapPoint at the beginning of my code. Is there some interference in a sense that the program does not know how to get the data from the Excel worksheet? I must also say that I am quite certain the pathway is correct because if I for example use something like
Dim eapp as New Excel.Application
Dim wbk As Excel.Workbook = eapp.Workbooks.Open("C:\users\admin\Desktop\Clienti.xlsx")
Dim sh As Excel.Worksheet = wbk.Sheets(1)
and I continue to go through every cell with sh.Range, it works without a problem. It is true that in the first pathway, I also want to "select" the cells I am interested in directly, so that the data can be uploaded in MapPoint, but I still think it should not be an issue. Any ideas?

Related

Debug doesn't work with library references - Errormsg "Cant enter break mode at this time"

I wrote a macro in Excel VBA to make users send their Excel-File via E-Mail automatically back to me.
To use this macro every user must install the Outlook Library. For this I created the function add_outlook. If I try to run the function it works.
The only problem occurring is that VBA doesn't let me debug. When stepping through the code I get the Errormsg "Cant enter break mode at this time"
Is there a workaround or fix?
Thanks a lot!
Option Explicit
Public Function add_outlook()
'DEBUGGING DOESNT WORK
'late binding
Dim vbProj As Object
Set vbProj = ThisWorkbook.VBProject
Dim vbRefs As Object
Set vbRefs = vbProj.References
Dim vbRef As Object
'Libary GUID and Data
Dim libname As String
libname = "Outlook"
Dim guid As String
Dim major As Long
Dim minor As Long
Dim exists As Boolean
guid = "{00062FFF-0000-0000-C000-000000000046}"
major = 9
minor = 6
'Reference cleanup function
For Each vbRef In vbRefs
If vbRef.Name = libname Then
'problem occurs here
vbRefs.Remove Reference:=vbRef
End If
Next
'add Ref
vbRefs.AddFromGuid guid:=guid, major:=major, minor:=minor
End Function
Solution:
Sub Workbook_Open()
Call add_outlook
End Sub
Cant step through code
Get reference when Workbook_open event is triggered

Importing an excel file to existing DataGridView in VB.Net

I have a problem with my below code which freezes and does nothing while running.
INFRASTRUCTURE:
Visual Studio 2017
.NET Framework 4.7.2
OS: Windows 7
GOAL:
I have a DataGridView which has 3 columns, I'm listing some information in this DataGridView from another source to fill first and second row.
The first row is the parameter name and the second one is this parameter's current value.
The third column is not filling in this process, I will fill that 3rd column from my database Excel file to compare the parameter's current value with the database value stored in Excel. This is where my problems start.
I'm trying to use below code for filling DataGridView parameter value from Excel sheet which I'm using a database;
Some parameters not stored in an Excel sheet so in fact, I need a function like vlookup to map data with the parameter name.
In excel A column is my parameter name and B column is the parameter's database value.
I'm trying to import this excel and trying to match parameter names in DataGridView and Excel if the parameter name is same it should writing Excel parameter value to a 3rd column in DataGridView.
Public Class BuildImportExcel
Public Shared Sub NewMethod(ByVal dgv As DataGridView)
Dim ofd As OpenFileDialog = New OpenFileDialog With {
.Filter = "Excel |*.xlsx",
.Title = "Import Excel File"
}
ofd.ShowDialog()
Try
If ofd.FileName IsNot "" Then
Dim xlApp As New Excel.Application
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!")
Else
Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open(ofd.FileName)
Dim xlSheet As Excel.Worksheet = CType(xlBook.ActiveSheet, Excel.Worksheet)
For i = 0 To dgv.Rows.Count
If dgv.Rows(i).Cells(0).Value IsNot "" Then
Dim look As Boolean = True
Dim found As Boolean = False
Dim rowLook As Integer = 2
Dim rowFound As Integer = 0
While look = True
If xlSheet.Range("A" & rowLook).Value IsNot "" Then
If xlSheet.Range("A" & rowLook).Text Is dgv.Rows(i).Cells(0).Value Then
found = True
rowFound = rowLook
End If
Else
look = False
End If
rowLook = rowLook + 1
End While
If found = True Then
dgv.Rows(i).Cells(2).Value = xlSheet.Range("B" & rowFound).Text
End If
End If
Next
xlApp.Quit()
Release(xlSheet)
Release(xlBook)
Release(xlApp)
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Shared Sub Release(ByVal sender As Object)
Try
If sender IsNot Nothing Then
Marshal.ReleaseComObject(sender)
sender = Nothing
End If
Catch ex As Exception
sender = Nothing
End Try
End Sub
End Class
But the problem is it's not working freezing I thought parameter list about 200 rows so it causes to freezing and try it with small portions like 5 parameters and still same. Seems something wrong and I couldn't find it.
Also is it logic way to match them with that method or do you suggest anything like OLEDB connection?
EDIT:
I turn off Option Strict and then change IsNot to operator <> and then it start to work but i'd like to use Option Strict On how can i handle this operators?
You are making this more difficult than it has to be. A questionable and problematic area is the While look = True loop. This assumes a lot and, in my test, will cause the code to freeze often. The main problem here is that the code is (somewhat) looping through the rows from the Excel file. The issue here is that you do not KNOW how may rows there are! There is no checking for this and the code will fail on the line…
If xlSheet.Range("A" & rowLook).Value IsNot "" Then
When you reach the bottom of the given Excel file and it never finds a match.
Another issue is the line…
If xlSheet.Range("A" & rowLook).Text Is dgv.Rows(i).Cells(0).Value Then
This is ALWAYS going to fail and will never be true. My understanding…
The Is operator determines if two object references refer to the same
object. However, it does not perform value comparisons.
Given this, I recommend you simplify things a bit. Most important are the loops through the Excel worksheet which is “expensive” and if there is a lot of rows, you may have a performance problem. When I say a lot of rows, I mean tens of thousands of rows…. 200 rows should be fine.
It would un-complicate things if we could tell how many rows from the worksheet are returned from….
Dim xlSheet As Excel.Worksheet = CType(xlBook.ActiveSheet, Excel.Worksheet)
This is basically an Excel Range, and it is from this Range that we can get the number of rows in this excel range with…
Dim totalExcelRows = xlSheet.UsedRange.Rows.Count
Now, instead of the complicated and problematic While loop, we can replace it with a simple for loop. This will eliminate some variables and will keep the loop index in row range in the excel file.
I hope this makes sense… below is an example of what is described above.
Public Shared Sub NewMethod(ByVal dgv As DataGridView)
Dim ofd As OpenFileDialog = New OpenFileDialog With {
.Filter = "Excel |*.xlsx",
.Title = "Import Excel File",
.InitialDirectory = "D:\\Test\\ExcelFiles"
}
ofd.ShowDialog()
Try
If ofd.FileName IsNot "" Then
Dim xlApp As New Excel.Application
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!")
Else
Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open(ofd.FileName)
Dim xlSheet As Excel.Worksheet = CType(xlBook.ActiveSheet, Excel.Worksheet)
Dim totalExcelRows = xlSheet.UsedRange.Rows.Count
For i = 0 To dgv.Rows.Count
If dgv.Rows(i).Cells(0).Value IsNot Nothing Then
For excelRow = 1 To totalExcelRows
If xlSheet.Range("A" & excelRow).Text.ToString() = dgv.Rows(i).Cells(0).Value.ToString() Then
dgv.Rows(i).Cells(2).Value = xlSheet.Range("B" & excelRow).Text
Exit For
End If
Next
Else
Exit For
End If
Next
xlApp.Quit()
Marshal.ReleaseComObject(xlSheet)
Marshal.ReleaseComObject(xlBook)
Marshal.ReleaseComObject(xlApp)
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Excel spreadsheet created in VB.NET gets locked and is unable to be deleted

In VB.NET I'm reading an Excel Spreadsheet like so:
Dim xlApp As Excel.Application = New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(Server.MapPath(SavePath & sFilename))
Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Sheets(1)
Dim eRange As Excel.Range = xlWorkSheet.Range("C3:C" & xlApp.Rows.End(Excel.XlDirection.xlDown).Row)
Dim bottomRange As Integer = xlApp.Rows.End(Excel.XlDirection.xlDown).Row
...
After I open it up and read some data I want to close it and make it possible for someone to delete it MANUALLY(clicking on the file and pressing delete) afterwards, so after looking around I found this sub that is supposed to release the locks on the objects that I create:
Private Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel =
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
Loop While intRel > 0
Catch ex As Exception
MsgBox("Error releasing object" & ex.ToString)
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
But whenever I create a spreadsheet and close my program and then try to delete the file MANUALLY, not in my program, I get the error:
The action can't be completed because the file is open in Excel 2016.
Choose the file and try again.
This error even persists after I have closed my program. Can someone please help me out in figuring out how to release these locks Excel has on the files? I should also note that I pass xlApp to the sub.

How to use OpenOffice Spreadsheet to get an image from an excel file

I have a code that exports image from excel into a picturebox and here it is.
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = False
Dim xlsBook As New excel.Workbook
Dim xlsSheet As New excel.Worksheet
Dim rowlocation As Integer
Dim columnlocation As Integer
Dim celladdress As String
Set xlsBook = appExcel.Workbooks.Open(Text1.Text)
Set xlsSheet = xlsBook.Worksheets("Sheet1")
Dim x As excel.Shapes
For Each x In xlsSheet.Shapes
x.Copy
Picture1.Picture = Clipboard.GetData(vbCFBitmap)
Text2.Text = x.Name
rowlocation = x.TopLeftCell.Row
columnlocation = x.TopLeftCell.Column
celladdress = xlsSheet.Cells(x.BottomRightCell.Row + 1, x.TopLeftCell.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
MsgBox ActiveSheet.Range(celladdress)
Next
End If
and unfortunately this code wont work on my friends PC becuase he does not have an Excel installed but he has OpenOffice spreadsheet. I tried to open the Excel in Openoffice then the file opens now my goal is how can i convert the code above in OpenOffice? I mean run the code for OpenOffice files.
This is my code but its not working
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\Testing.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
For i = 0 To 2
For j = 0 To 9
' Form1.Image1.Picture = Clipboard.GetData
Form1.Image1.Picture = LoadPicture(oDrawPage)
Next
Next
TYSM for future help
This is the latest code in VB6 and it has an error saying vnd.sun.star is missing
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Dim Cell As Object
Dim sGraphicUrl As String
Dim oDisp
Dim oFrame
Dim opos As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set osize = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size")
Set opos = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Point")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\ACE Express - Fairview_Sample PC of Gondola.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oimage = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
Set oimage = oDrawPage.getByIndex(0)
Image1.Picture = LoadPicture(oimage.GraphicURL)
Here is the output of the unzip picture
I do not know exactly what your code does, because I normally do not use Microsoft Office. However it looks like this task can be accomplished using OpenOffice Basic. One of the best places to learn OpenOffice Basic is Andrew Pitonyak's Macro Document.
To start with, look at section 5.9.5. Convert all linked images.
EDIT:
To do this in Calc, first I went to Tools -> Macros -> Organize Dialogs and created a dialog named "ImageViewerForm" with an image control named "MyImageControl".
Then I went to Tools -> Macros -> Organize Macros -> OpenOffice Basic and added the following code:
Sub ShowImageViewerDialog
oDoc = ThisComponent
oDlg = CreateUnoDialog(DialogLibraries.Standard.ImageViewerForm)
oControl = oDlg.Model.MyImageControl
oDrawPage = oDoc.getDrawPages().getByIndex(0)
oImage = oDrawPage.getByIndex(0)
oControl.ImageURL = oImage.GraphicURL
oDlg.execute()
End Sub
To run the code, go to Tools -> Macros -> Run Macro. Here is the result:
The "Next Image" button should be fairly straightforward to implement by adding an event handler.
For documentation, see GraphicObjectShape and UnoControlButtonModel. But mostly I just used the MRI tool to figure it out.
EDIT 2:
Regarding the error you posted, the GraphicURL property in this case is a string that references an in-memory object, not a filepath. An example of the string is shown here: https://www.openoffice.org/api/docs/common/ref/com/sun/star/graphic/XGraphicObject.html.
So it is not suitable for passing to LoadPicture, which takes a filename.
Perhaps you can get the actual image data from oImage.Bitmap or oImage.Graphic. Use MRI to see what attributes are available.
For example, it looks like there is a getDIB() method that might work like this:
Form1.Image1.Picture = oImage.Bitmap.getDIB()
One more idea: Instead of using an Office application, you could write code that unzips the file and reads each image in the Pictures subdirectory.
I have never tried it but according to the docs you can control Open Office through COM automation.
Your VB6 code above is controlling Excel through COM automation. You could install Open Office on your machine, and see whether you can automate Calc from VB6 to open a spreadsheet and extract an image. I don't know whether it allows that. Excel COM automation is very powerful and allows you to do almost anything, Open Office may not be as powerful (I don't know).
I would advise thinking carefully about what problem you are trying to solve and whether there's another approach entirely!

Excel process running after closing VB.NET [duplicate]

I am creating an excel file using interop.excel and the process is not closing.
This is the code i am trying to use.
Private Sub converToExcel(fileLoc As String, ds As DataSet)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkBooks As Excel.Workbooks
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.Application
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Columns.NumberFormat = "#"
xlWorkSheet.Cells(i + 1, j + 1) = String.Format("{0}", ds.Tables(0).Rows(i).Item(j).ToString())
Next
Next
xlWorkSheet.SaveAs(fileLoc)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlWorkBooks)
releaseObject(xlApp)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
I think i am missing a COM object but cant seem to find a solution.
Also as a note, this is running on 64-bit Windows 8.
Any help would be great!
Thanks
Manual memory management like this just never works. This is a problem that's been known for very a long time and the core reason that garbage collectors were invented. Programmers just forever forget to release memory.
It gets extra hard when you can't see the memory being used. Which is certainly the case in your code, the xlWorkSheet.Cells(i + 1, j + 1) expression uses no less than three references. One for the range object returned by the Cells property, one for a sub-range object selected by i+1 and another for the sub-range object selected by j+1. Very nice syntax sugar provided by the VB.NET language, writing COM code without it is pretty doggone painful. But not helpful to let you see the references. Not only can't you see it in your source code, there is absolutely nothing the debugger can do to help you see them either.
This is very much a solved problem in .NET, it has a garbage collector and it can see everything. The most basic problem is that you don't give it a chance to solve your problem. The mistake you made is that you stopped. Probably by setting a breakpoint on the last statement and then looking in Task Manager and seeing Excel.exe still running. Yes, that's normal. Garbage collection is not instant.
Calling GC.Collect() is supposed to make it instant, but that doesn't work in the specific case of running the Debug build of your project. The lifetime of local variables gets then extended to the end of the method, help you see them in the Autos/Locals/Watch window. In other words, GC.Collect() doesn't actually collect any of the interface references. More about that behavior in this post.
The simple workaround is to not stop. Keep doing useful things to give the garbage collector a reason to run. Or letting your program terminate since it is done, Excel terminates when the finalizer thread runs for the last time. Which works because the local variables that had the references are not in scope anymore.
But everybody wants the instant fix anyway. You get it by deleting all the releaseObject() calls. And doing it like this instead:
converToExcel(path, dset)
GC.Collect()
GC.WaitForPendingFinalizers()
Or in other words, force a collection after the method has returned. The local variables are no longer in scope so they can't hold on to an Excel reference. It will now also work when you debug it, like it already did when you ran the Release build without a debugger.
Try System.Runtime.InteropServices.Marshal.FinalReleaseComObject, that should help... also you should call xlWorkBook.Close() and xlapp.quit, if I recall correctly. First call them and then set them to nothing.
The GC.Collect makes not much sense where you placed it, if anything you should call it after you return from converToExcel. Also you may need to wait for finalizers to run. Personally I think Hans' answer is the way to go, but I know from personal experience writing office addins in C# that sometimes its necessary to do manual reference counting, in particular when you need to be compatible with older office versions. (There are many documented problems, in particular when handling events from office, which can only be reliably solved by manual reference counting. Also some COM libraries don't like at all when released in the wrong order by GC, but thats not the case with office.)
So on to the actual problem in your code: there are three intermediate COM objects not released here:
xlWorkBook.Sheets returns a collection of type Excel.Sheets
xlWorkSheet.Columns returns a COM object of type Excel.Range
xlWorkSheet.Cells also returns an Excel.Range object
Besides this, if Marshal.ReleaseComObject throws an exception you did something wrong in your manual reference counting, therefore I wouldn't wrap it in an exception handler. When doing manual reference counting you must release every COM object once for every time it crosses the COM->NET boundary, meaning the Excel.Range objects need to be released in every iteration of the loop.
Here's code which properly terminates Excel for me:
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Private Sub converToExcel(fileLoc As String, ds As DataSet)
Dim xlApp As New Excel.Application
Dim xlWorkBooks As Excel.Workbooks = xlApp.Workbooks
Dim xlWorkBook As Excel.Workbook = xlWorkBooks.Add(System.Reflection.Missing.Value)
Dim xlWorkSheets As Excel.Sheets = xlWorkBook.Sheets
' accessing the sheet by index because name is localized and your code will fail in non-english office versions
Dim xlWorkSheet As Excel.Worksheet = xlWorkSheets(1)
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
' couldn't this be moved outside the loop?
Dim xlColumns As Excel.Range = xlWorkSheet.Columns
xlColumns.NumberFormat = "#"
Marshal.ReleaseComObject(xlColumns)
Dim xlCells As Excel.Range = xlWorkSheet.Cells
xlCells(i + 1, j + 1) = ds.Tables(0).Rows(i).Item(j).ToString()
Marshal.ReleaseComObject(xlCells)
Next
Next
xlWorkSheet.SaveAs(fileLoc)
'xlWorkBook.Close() -- not really necessary
xlApp.Quit()
Marshal.ReleaseComObject(xlWorkSheet)
Marshal.ReleaseComObject(xlWorkSheets)
Marshal.ReleaseComObject(xlWorkBook)
Marshal.ReleaseComObject(xlWorkBooks)
Marshal.ReleaseComObject(xlApp)
End Sub
If you want to be extra careful you'd want to handle exceptions from the office API and call ReleaseComObject inside finally-clauses. It can be helpful to define a generic wrapper and write using-clauses instead of try-finally (make the wrapper a structure not a class so you don't allocate the wrappers on the heap).
Finally solved :)
Private Function useSomeExcel(ByVal Excelfilename As String)
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim objWorkSheets As Excel.Worksheet
Dim datestart As Date = Date.Now
objExcel = CreateObject("Excel.Application") 'This opens...
objWorkBook = objExcel.Workbooks.Open(Excelfilename) ' ... excel process
Dim dateEnd As Date = Date.Now
End_Excel_App(datestart, dateEnd) ' This closes excel proces
End Function
use this method
Private Sub End_Excel_App(datestart As Date, dateEnd As Date)
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
This method closes especific process opened.
'Get the PID from the wHnd and kill the process.
' open the spreadsheet
ImportFileName = OpenFileDialog1.FileName
excel = New Microsoft.Office.Interop.Excel.ApplicationClass
wBook = excel.Workbooks.Open(ImportFileName)
hWnd = excel.Hwnd
Dim id As Integer = GetWindowThreadProcessId(hWnd, ExcelPID)
Sub CloseExcelFile()
Try
' first try this
wBook.Saved = True
wBook.Close()
excel.Quit()
' then this.
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
excel = Nothing
' This appears to be the only way to close excel!
Dim oProcess As Process
oProcess = Process.GetProcessById(ExcelPID)
If oProcess IsNot Nothing Then
oProcess.Kill()
End If
Catch ex As Exception
excel = Nothing
Finally
GC.Collect()
End Try
End Sub
I did not see anyone properly address what was occuring and instead, tried to create work arounds for it.
What is happening here is that the workbook is prompting, in the background, to be saved. In your code, you're saving the worksheet and not the workbook. You can either trick it and set the saved state of the workbook to true or save the workbook before exiting the excel applicaiton.
I was also having this issue. The Excel process would run the entire time the application was open. By adding the xlWorkBook.Saved = True line, the process would end after the call to xlApp.Quit(). In my case, I did not need to save the excel file, only reference it for values.
Option #1 - Do not save the workbook:
xlWorkSheet.SaveAs(fileLoc)
xlWorkBook.Saved = True ' Add this line here.
'xlWorkBook.Close() ' This line shouldn't be necessary anymore.
xlApp.Quit()
Option #2 - Save the workbook to a new file:
'xlWorkSheet.SaveAs(fileLoc) ' Not needed
xlWorkBook.SaveAs(fileLoc) ' If the file doesn't exist
'xlWorkBook.Close() ' This line shouldn't be necessary anymore.
xlApp.Quit()
Option #3 - Save the workbook to an existing file:
'xlWorkSheet.SaveAs(fileLoc) ' Not needed
xlWorkBook.Save(fileLoc) ' If the file does exist
'xlWorkBook.Close() ' This line shouldn't be necessary anymore.
xlApp.Quit()
.Quit() Method:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.quit?view=excel-pia#Microsoft_Office_Interop_Excel__Application_Quit
.Saved() Method:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._workbook.saved?view=excel-pia#Microsoft_Office_Interop_Excel__Workbook_Saved
It's just as simple as adding this line in your code, just after opening the Workbook:
oExcel.displayalerts = False
Although many of the answers here did indeed result in EXCEL.EXE being closed, they either resulted in a prompt from AutoRecover upon manually opening the workbook or required redundant usage of Garbage Collection.
For my purposes, I'm creating workbooks with ClosedXML (because it's significantly faster for what I need) and using Excel Interop to set the workbook password (for reasons I won't get into here); however, Excel Interop doesn't actually close EXCEL.EXE for me when Application.Quit() is called, even after the method has returned and the objects are long out of scope.
Hans' answer beautifully explained how to get Excel to actually quit without forcefully killing the process. Using that explanation, I created a small class to help avoid redundant Garbage Collection calls as I have multiple areas of code that require this functionality.
Option Strict On
Option Explicit On
Imports Microsoft.Office.Interop.Excel
Public MustInherit Class ExcelInteropFunction
Public Shared Sub SetWorkbookPassword(WorkbookPathAndName As String, NewPassword As String)
SetPassword(WorkbookPathAndName, NewPassword)
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
Private Shared Sub SetPassword(WorkbookPathAndName As String, NewPassword As String)
Dim xlWorkbook As Workbook = New Application() With {
.Visible = False,
.ScreenUpdating = False,
.DisplayAlerts = False
}.Workbooks.OpenXML(WorkbookPathAndName)
xlWorkbook.Password = NewPassword
xlWorkbook.Save()
xlWorkbook.Application.Quit()
End Sub
End Class
The reason the actual work is being done in a private method is to allow the method that the Excel Interop variables are in to return and go fully out of scope before garbage collection is manually triggered. Without it structured this way, I'd have to add GC calls in several areas of code, so this helps avoid some redundancy and ensures I don't forget a GC call after setting a workbook password.
Dim xlp() As Process = Process.GetProcessesByName("EXCEL")
For Each Process As Process In xlp
Process.Kill()
If Process.GetProcessesByName("EXCEL").Count = 0 Then
Exit For
End If
Next

Resources