Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA - Excel sometimes fails to close properly

Tags:

excel

vba

I have a macro in outlook, which helps runs statistics in an excel workbook. However sometimes it fails to close it properly, and ends up ruining the process, since the workbook is open still, when i run it next time.

This is my method for closing it.

 Dim xlApp As Object
 Dim xlWB As Object
 Dim xlSheet As Excel.Worksheet

 Set xlApp = New Excel.Application
 Set xlWB = xlApp.Workbooks.Open(strpath)

...

xlWB.Save
xlWB.Close savechanges:=True
xlApp.Quit

Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing

From my understanding it should do it.

like image 791
ZZA Avatar asked Feb 03 '26 04:02

ZZA


1 Answers

Did you turn the displayalerts off? Use:

xlApp.DisplayAlerts = False

after you instanciate the Excel application. That prevents Excel from asking for user input ("are you really sure you want to ...?). Such popup could prevent Excel from closing.

Happened to me more than once on an invisable application.

like image 51
Jzz Avatar answered Feb 04 '26 17:02

Jzz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!