Search code examples
python-3.xvbaxlwings

xlwings: Unable to import functions (UDFs) via xlwings ribbon


I'm trying to import a user-defined function(UDF) via xlwings but am encountering an issue. Upon pressing the import functions button in the xlwings ribbon, I receive the following run time error:

`Run_time error '1004 

`Method of 'VBProject' of object '_Workbook' failed.'

According to the VBA debugger, the below module contains the problem:

Sub ImportXlwingsUdfsModule(tf As String)
     ' Fallback: This is called from Python as direct pywin32 calls were 
     '  sometimes failing, see comments in the Python code
     On Error Resume Next
     ActiveWorkbook.VBProject.VBComponents.Remove 
     ActiveWorkbook.VBProject.VBComponents("xlwings_udfs")
     On Error GoTo 0
     **ActiveWorkbook.VBProject.VBComponents.Import tf**
End Sub

The .py file containing the UDF is saved in the same folder as the calling .xlsm workbook.

How do I rectify this so I can utilize UDFs?


Solution

  • Thanks to the xlwings team for providing a link that helped me resolve the issue.

    One needs to ensure that Trust Access to VBA object model is enabled.

    See: https://docs.xlwings.org/en/stable/udfs.html#one-time-excel-preparations