Call Macro in Excel Sheet with XLWings

Viewed 149

Can anyone help me understand what I'm doing wrong calling macros from Python? If I open the a sheet manually, I can run Macros (via the toolbar) no problem. If I open it programattically, it tells me Macros are disabled!

I have a sheet with a module, as such:

Project Layout

The module contains the following:

Sub test()
    Range("A1").Value = "This is a test."
End Sub

Every time I call the method, it fails. Here's what I've tried, along with the errors I got:

Open Sheet:

with xw.App(visible=True, add_book=False) as app:
    wb = app.books.open(r"test1.xlsm")
    ws: xw.Sheet = wb.sheets["Sheet1"]

Attempts:

wb.macro("test")()

  • "Cannot run the macro ''test1.xlsm'!test'. The macro may not be available in this workbook or all macros may be disabled."

app.macro("test")()

  • "Cannot run the macro 'test'. The macro may not be available in this workbook or all macros may be disabled."

app.macro("'test1.xlsm'!test")()

  • "Cannot run the macro ''test1.xlsm'!test'. The macro may not be available in this workbook or all macros may be disabled."

app.api.Application.run("test")

  • Deadlocks

wb.api.Application.run("test")

  • Deadlocks

wb.macro('Module1.test')()

  • Because of your security settings, macros have been disabled. To run macros, you need to reopen this workbook, and then choose to enable macros.

Excel Security

And just in case this is a thought: I enabled everything possible in the Trust Center.

Trust Center

0 Answers
Related