2

I'm trying to get the return value from an Excel function that i called from a python. I always get a None value.

Here is the excel function :

Public Function GetString()
    GetString = "aa"
End Function

And Here is the call :

xlApp.Run('ThisWorkbook.GetString')

Returned :None

I have no problem getting in the specified function since I tried to modify a cell value from it and it worked.

Am I missing anything ?

1 Answer 1

0

It is not clear how you assigned your variable xlApp. Assuming code like

import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
xlApp = xlApp.Application

consider:

  1. You have to assign the result to a variable, as in

    myStr = xlApp.Run('ThisWorkbook.GetString')

  2. You seem to have placed the user defined function in ThisWorkbook, in the Excel file. Unless this is intentional, open the Excel file, insert a Module in the Visual Basic Editor, and move there your Function. Modify your call from Python accordingly.

  3. You may want to define the function as

    Public Function GetString() as String

See How to call Excel VBA functions and subs using Python win32com?

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

2 Comments

I forgot the return type but I don't get why it wouldn't work from the workbook but on a module.
I was not sure if that would make a difference. At the least, it is a matter of good coding practice (which would not explain your second question). I have seen a couple of strange things when moving code this way, so I though it might have been an additional factor in you problem.

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.