0

I wish I could use python to execute the Excel macro, so I tried to use the package xlwings to implement it.

The OS of my laptop is macOS Catalina (ver.: 10.15.7), my compiler is PyCharm (ver.: 2021.2.3), my Python version is 3.8.8, I used Anaconda (Ver.: 22.11.1) as my interpreter, my excel version is 16.66.1 (Microsoft Excel for Mac).

I faced the error "Command Error -1743: The User has declined permission" when I tried to use this package originally, and I solved this issue by installing an old compiler & using the old version of the compiler to run my code. My privacy setting for automation in the app Setting was shown below: (This is NOT the question I want to ask, but I'm not sure if it is also related to the issue I faced, so I still attached it here. I had uninstalled the old version of my compiler already.)

New Privacy Setting

I wish I could implement an existing macro (called Hi) in an existing Excel file (called [StakeOverflow]HelloWorld.xlsm) through Python (represented as MY_PYTHON_FILE.py below), like the snapshot below:

Ideal Outcome

My Excel file and my python code were stored on OneDrive. My macro code was shown below:

Sub Hi()

MsgBox "Good morning!"

End Sub

My python code was shown below:

import xlwings as xw
import time

wb = xw.Book('/Users/<MY NAME>/OneDrive/MY PATH DETAILS/[StakeOverflow]HelloWorld.xlsm')
time.sleep(10)
app = wb.app
macro_vba = app.macro("Hi")
macro_vba()

The code looks really simple, but I still faced the error. My Excel was not opened automatically, and I even could not open the Excel file manually thereafter. The error was shown below:

/Users/.../.conda/envs/Program/bin/python "/Users/.../OneDrive/.../MY_PYTHON_FILE.py"

Traceback (most recent call last):
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 4914, in open
    impl= self.impl(name)
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/_xlmac.py", line 366, in __call__
    raise KeyError(name_or_index)
KeyError: '[stakeoverflow] helloworld.xlsm'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/appscript/reference.py", line 482, in __call___
    return self.AS_appdata.target().event (self._code, params, atts, codecs=self.AS_appdata).send(timeout, sendflags) 
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/aem/aemsend.py", line 92, in send
    raise EventError(errornum, errormsg, eventresult)
aem.aemsend.EventError: Command failed: Parameter error. (-50)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/.../OneDrive/.../MY_PYTHON_FILE.py", line 18, in <module>
    wb = xw.Book('/Users/.../OneDrive/.../[stakeoverflow] helloworld.xlsm')
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 876, in __init__
    impl= app.books.open(
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/main.py", line 4921, in open
    impl = self.impl.open( 
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/xlwings/ xlmac.py", line 420, in open
    self.app.xl.open_workbook(
  File "/Users/.../.conda/envs/Program/lib/python3.9/site-packages/aeosa/appscript/reference.py", line 518, in __call__
    raise CommandError(self, (args, kargs), e, self.AS_appdata) from e 
appscript.reference.CommandError: Command failed:
        OSERROR: -50
        MESSAGE: Parameter error.
        COMMAND: app(pid=1647).open_workbook (workbook_file_name='/users/.../onedrive/.../[stakeoverflow] helloworld.xlsm', update_links=k.do_not_update_links, read_only=None, format=None, password=None, write_reserved_password=None, ignore_read_only_recommended=None, origin=None, delimiter=None, editable=None, notify=None, converter=None, add_to_mru=None, timeout=-1)


Process finished with exit code 1

I tried to use terminal to run my python code, but I could not solve the problem, either.

I tried to open the Excel file manually thereafter, the error was shown below:

Excel cannot open the file ’[StakeOverflow]HelloWorld.xlsm’ 
because the file format or file extension is not valid. Verify 
that the file has not been corrupted and that the file 
extension matches the format of the file.

I tried to Google this error, but few solutions was found. It seems that it is related to the issue of external storage location. I tried to create another Excel file with the same name & macro code on my desktop and try again, and the error would disappear. (However, our company stored the files on OneDrive, so I wish I could utilise the file online.)

Just wondering if anyone here faced this situation before?

5
  • Please avoid using images for code and/or error displays. Wherever possible these should be pasted as text and formatted as code. Also it helps to not clutter the question with too many images, where possible link rather than display the image in the question. Commented Jan 3, 2023 at 3:15
  • There are a couple of links to issues with suggestions here: github.com/xlwings/xlwings/issues/1323 Commented Jan 3, 2023 at 7:11
  • @FelixZumstein I checked those articles already, but it seems that most of them are related to the issue “Command Error -1743” rather than the issue “Command failed: Parameter error. (-50)”. (It seems that my problem is that my python code could not open my file due to other reason and would corrupt the excel file if the excel file was stored in OneDrive.) I also tried to follow what those articles said and get the permission of System Events for Pycharm. (I updated my privacy setting snapshot in the question above.) I found the solution today, and I added below. Anyway, still thanks for your Commented Jan 3, 2023 at 14:36
  • Oh I see: try with wb.macro("Hi") instead of app.... Commented Jan 3, 2023 at 14:44
  • @FelixZumstein I tried wb.macro("Hi") with the Excel file which name contains special characters, I faced another error Command failed: The object you are trying to access does not exist (-1728); however, I tried the same code with the Excel file which name did not contain special characters, no error occurred. I don't know why, but at least I solve the question. Anyway, thanks for your help! Commented Jan 3, 2023 at 15:04

1 Answer 1

1

I found the answer by myself today. The issue is related to the naming issue rather than the permission issue.

As we could see that the error is KeyError: '[stakeoverflow] helloworld.xlsm', which implies that the problem is here. (Maybe because the system could not find the Excel file with this name.)

I tried to change the name from [stakeoverflow] helloworld.xlsm to helloworld.xlsm, and the error was gone. It seems that when xlwings want to open an Excel file, it would check the validity of the file name and change the name into smaller cases. If the file name contains special characters which are not allowed (e.g., "[]"), then the error would occur.

Notice that I could store the Excel file with these special characters in our laptop & OneDrive, but xlwings did not accept them.

Hope it is helpful to those who face this issue when using xlwings!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.