1
votes

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.

2
See my answer here for an example of how to make sure an instance of excel is not left open even if your program errors.RubberDuck

2 Answers

2
votes

You need to quit the instance you started. Replace

Excel.Application.Quit

with

ExcelApp.Quit
1
votes

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