David
David

Reputation: 13

VBA - Close window

I have a macro that creates 17 workbooks in a loop. When it's done working on a workbook it closes it and goes to the next with:

ActiveWorkbook.Save
ActiveWindow.Close

This has always worked fine until recently. Suddenly on loop nr. 7 it say (the 6 loops before still working fine):

Run-time '-2147417848 (80010108)':

Method 'Close' of objekt 'Window' failed

I can delete "ActiveWindow.Close" and close all the workbooks manually to make the macro work. But still...

Any knows why it does this?

Upvotes: 1

Views: 16957

Answers (3)

Jonathan
Jonathan

Reputation: 1

I use the number of the window... i.e. I know this will be Window #1, so I refer to it that way. Example:

Windows(1).Close SaveChanges:=False

Upvotes: 0

Fionnuala
Fionnuala

Reputation: 91376

As per comment.

A wait period may solve this problem.

Upvotes: 5

Gaijinhunter
Gaijinhunter

Reputation: 14685

I'm not sure why you are using ActiveWindow.Close, perhaps that has something to do with it. The proper way to close a book (as far as I know) is:

Workbooks(excelFile).Close SaveChanges:=True

Sometimes some wacky things happen when refering to the active object, so it's best to be explicit.

Upvotes: 4

Related Questions