Milan
Milan

Reputation: 35

word vba wont close excel application

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.

Upvotes: 1

Views: 3625

Answers (2)

Milan
Milan

Reputation: 35

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

Upvotes: 1

Andy G
Andy G

Reputation: 19367

You need to quit the instance you started. Replace

Excel.Application.Quit

with

ExcelApp.Quit

Upvotes: 2

Related Questions