Matthew Ward
Matthew Ward

Reputation: 1

When running xlwings from VBA, I'm getting an "invalid procedure call or argument error". Why, and how do I fix it?

I'm trying to run a simple python script from VBA using xlwings, but I am getting an error "Run time error '5': Invalid procedure call or argument".

I suspect that this is because it is unable to find the correct Python file (microsoft help on this error says "if an attempt is made to call a procedure that isn't valid on the current platform")

I have gone through the xlwings documentation and various similar forum posts, but have not been able to crack it.

The Excel book is called hello.xlsm, and aims to call hello.py (located in the same folder) using the VBA code here:

Sub HelloWorld()
    RunPython "import hello; hello.world()"
End Sub

hello.py looks like this:

# hello.py
import numpy as np
import xlwings as xw

def world():
    wb = xw.Book.caller()
    wb.sheets[0]['A1'].value = 'Hello World!'

The xlwings configuration file looks like this: NOTE: I have also tried with C:\hello, and pythonw.exe as suggested in a few other posts

"PYTHONPATH","C:\hello" 
"INTERPRETER_WIN","C:\Users\matt.ward\AppData\Local\Microsoft\WindowsApps\python3.12.exe"

I can also confirm that I have the xlwings reference module ticked within Excel.

I would expect this to run the world function within hello.py, but I get the error message, which suggests to me that it is unable to actually get into running the Python code.

Has anyone come across this before? Any suggestions for where to look?

Upvotes: 0

Views: 74

Answers (0)

Related Questions