Skip to content

Support calling globally defined macro functions with arguments from form controls #121

Description

@liviofetahu-betm

Issue Description.

When one defines a Python function which takes input arguments and decorates it with xloil.func(macro=True, command=True) so that it can be executed as an Excel VBA Macro in a Macro-Enabled Worksheet File (.xlsm), one is forced to use the setting WorkbookModule="*.py" in the xlOil.ini file in order for the Python function to be exposed as an Excel VBA Macro successfully.
If one needs this Python function to be exposed globally to Excel sessions (not just the workbook with the same filename as the Python module file), disabling the setting WorkbookModule="*.py" leads to the Python function not being recognized as a VBA Macro when one supplies the input arguments to the function in the Excel Developer Macros dialog box.
The article at (https://www.teachexcel.com/excel-tutorial/1893/pass-arguments-to-a-macro-called-from-a-button-or-sheet-in-excel#:~:text=We%20must%20type%20in%20the,to%20send%20to%20the%20macro.&text=Note%3A%20the%20text%20is%20surrounded,surrounded%20by%20single%20quotation%20marks.) provides instructions on how to execute Excel VBA Macros which take input arguments from the Developer ribbon, Macros dialog box. Regarding the example given below, the full literal string to run the macro would be
'write_single_kwarg_to_active_worksheet [{"username","liviofetahu-betm";"",""}]'
and note that the single quote characters at the beginning and at the end are required.

Reproducing Code Example

import xloil

@xloil.func(macro=True, command=True)
def write_single_kwarg_to_active_worksheet(**kwargs):
    kwarg_name = list(kwargs.keys())[0]
    kwarg_value = kwargs[kwarg_name]
    current_active_worksheet = xloil.active_worksheet()
    current_active_worksheet.at("A1").value = kwarg_name
    current_active_worksheet.at("B1").value = kwarg_value

Error message

When using the setting WorkbookModule="*.py" in the xlOil.ini file, the Run button in the Macros dialog box appears enabled and clicking it results in writing to the 2 cells in the active worksheet.
However, when the setting WorkbookModule="*.py" is disabled, the Run button is still frozen even after typing the call to the function in the Macro name text box.

xloil/Python/Excel/Windows version information

0.19.1 / sys.version_info(major=3, minor=11, micro=3, releaselevel='final', serial=0) / Version 2407 Build 16.0.17830.20210 / 10.0.19045.4894
xloil_excel_vba_macro_not_runnable

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions