Home » excel » word vba wont close excel application

word vba wont close excel application

Posted by: admin May 14, 2020 Leave a comment

Questions:

How can I close excel aplication completely in word vba?

My code:

Dim ExcelApp As Object, ExcelBook As Object
Set ExcelApp = CreateObject("excel.Application")
Set ExcelBook = ExcelApp.Workbooks.Open(ActiveDocument.Path & "\data.xls")
'ExcelApp.Visible = True/False
ExcelApp.DisplayAlerts = False

'something with ExcelBook, just editing cells

ExcelBook.Close savechanges:=True
Excel.Application.Quit
Set ExcelBook = Nothing
Set ExcelApp = Nothing

When I open taskmanager there is still proces EXCEL running. Problem is, that I need to run macro multiple times and than my data.xls – excel file can be open read only.

How to&Answers:

You need to quit the instance you started. Replace

Excel.Application.Quit

with

ExcelApp.Quit

Answer:

I find out why I had this problem. While running code that uses Automation to control Microsoft Excel, Visual Basic does not release reference until you end the program.

When I close word, excel process is closed as well.

More info here:

http://support.microsoft.com/kb/178510/en-us