cunnane / xloil

xlOil provides framework for interacting with Excel in different programming languages (python & C++ currently)
Other
127 stars 11 forks source link

After using the xloil module,Excel always fail to close the last workbook normally. #22

Closed catbor closed 1 year ago

catbor commented 1 year ago
[2022-12-20 13:44:10.268] [logger] [error] [logging.py:25] Error during _import_and_scan: Traceback (most recent call last):   File "D:\anaconda3\lib\site-packages\xloil\register.py", line 129, in logged_func     return func(*args, **kwargs)   File "D:\anaconda3\lib\site-packages\xloil\importer.py", line 115, in _import_and_scan     result.append(_import_and_scan(m, addin))   File "D:\anaconda3\lib\site-packages\xloil\importer.py", line 106, in _import_and_scan     module = importlib.import_module(what)   File "D:\anaconda3\Lib\importlib\__init__.py", line 127, in import_module     return _bootstrap._gcd_import(name[level:], package, level)   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import   File "<frozen importlib._bootstrap>", line 991, in _find_and_load   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked   File "D:\anaconda3\lib\site-packages\xloil\importer.py", line 183, in exec_module     super().exec_module(module)   File "<frozen importlib._bootstrap_external>", line 843, in exec_module   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "D:\anaconda3\lib\site-packages\xloil\xloil_ribbon.py", line 346, in <module>     _ribbon_ui = xloil.ExcelGUI(ribbon=r''' RuntimeError: Caught an unknown exception!

Hello Steven, after using the xloil module, Excel will always fail to close the last workbook normally, this is my xloil log(xlOil.log), please help me to see how to solve it, thank you.

cunnane commented 1 year ago

This error message is occuring at startup when xlOil tries to load the ribbon. It seems to be an escaping COM error. I'll add some code to trap and print this error, but it won't stop the underlying cause and usually COM errors are not particularly descriptive. If you disable the xlOil ribbon I would imagine the problem would go away, although this is not an ideal fix of course!

Are you seeing another error on close? Currently your xlOil log just contains Error statements, whch suggests your loglevel is too high to provide much useful information. It also seems to contain old messages from the previous issue you posted - could you clear the log file then reproduce the error at Debug logging level?

airguru commented 1 year ago

I am observing the same problem. No error on workbook close, but during opening the ribbon load sometimes fails. This is my log on trace logging level, but it doesn't seem to show anything interesting. ribbon_error_log.txt. I have Excel 2010.

Restarting excel eventually fixes it (but I recall it can happen multiple times in a row). I could not find any common denominator that is correlated with this so far. I am ready to test it with a newer version of xloil that has even more logging.

cunnane commented 1 year ago

In xlOil v0.16, I've put some better error trapping around this, hopefully this will give us some clue as to the cause. Excel's COM interface can become 'busy' and reject extenal calls or otherwise behave strangely - it's very old code!

airguru commented 1 year ago

Tried it in v0.16 and you are completely right. That's what the error message says. ribbon_error_log.txt

I do have some spurious message boxes appearing during the start of my excel, which is caused by some failed add-in development of mine, but they appear all the time and most of the time xloil loads fine and I cannot see any correlation with the above error. I also tried specifically opening workbooks with "macros disabled" bar, "document recovery" sidebar, because I thought the xloil ribbon fail happens more frequently when these appear, but out of about 5-10 tries I could not make it fail again.

Is it possible to wait for the COM interface being not-busy?

cunnane commented 1 year ago

Thanks for checking! Sometimes the "busyness" appears random, but yes the document recovery bar definitely increases the probability!

xlOil is supposed to wait and retry, but I appear to have broken this feature in some cases.... I have a fix now and will let you know when it's deployed.

cunnane commented 1 year ago

I've fixed the COM busy retrying logic in v0.16.1, which is now available. Hopefuly this will fix the issue here.

airguru commented 1 year ago

Thanks a lot! I will be testing it!

airguru commented 1 year ago

In the past two weeks, no COM busy related errors have so far appeared on 0.16.1!

cunnane commented 1 year ago

Great news! I'll close this, please re-open if the issue re-surfaces.

ShieldTrade commented 1 year ago

Using windows 10, python 3.9, Excel 2021, xloil 0.17.17

I am also experiencing a few problems when closing an excel file.

1) my_code.xlsm has only udf Adder() Save and then close excel. Works Close excel without prior save. Not working. The pop up asking me if I want to save changes is closed without any action on my side. excel will be restarted in temporary mode when trying to reopen the file

2) my_code.xlsm has udf Adder()and udf pyClock() Closing excel ( with or without saving it ). Not working. Hang infinitely and I have to end task using task manager or it closes but on next opening excel will start in temporary mode.

my_code.py

import xloil as xlo
import datetime, asyncio

@xlo.func
def Adder(x, y):
    return x + y

@xlo.func
async def pyClock():
    while True:
        yield datetime.datetime.now()
        await asyncio.sleep(10)