Excel 2007 VBA Workbook closing then reopening

2019-09-02 01:47发布

In 2003 the workbook would just close. But now the same code is re-opening the workbook. There are some lines afterwards and then the sub ends. When the sub ends, the workbook_open event gets fired for the closed workbook, even though there is no code that opens any workbooks. The debugger is almost useless, it is not reproducing the bug if I execute everything step by step, in fact everything works fine when I use the debugger.

I use

ActiveWorkbook.Close False

For closing the workbook (from an add-in).

Help much appreciated.

2条回答
萌系小妹纸
2楼-- · 2019-09-02 02:26

I added a slight amount of code for Excel 2007 because of the annoying ribbon and Microsoft getting rid of custom menus. To somewhat compensate for this, I wanted to have the add-ins tab in the ribbon always visible for convenience, but I was using Application.OnTime and sendkeys to do it (because Microsoft didn't bother including an API with the stupid ribbon...). Well OnTime seems to have been the root of my troubles.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-02 02:39

Figured out how to get both. Workbook_beforeclose cancels the scheduled ontime event by calling:

Application.OnTime EarliestTime:=Now(), Procedure:="Name", Schedule:=False

but otherwise the ontime event gets executed.

If the event does not get cancelled before beforeclose is called, the workbook will close then re-open to run the scheduled ontime event (because the workbook is opened so briefly that ontime doesn't get a chance to run before I need to close it).

查看更多
登录 后发表回答