winpy32 throws -2146826246 when accessing a specific cell using WorkSheet.Range('A1').value

50 views Asked by At

I have a script that is reading a complex model in excel, using pywin32. In the first part of the script, model inputs are found from the input worksheet within the workbook through InputWorkSheet.Range('A1').value, and these return the appropriate cell values.

The model then runs, inside the excel workbook, and again I'm using pywin32 to access the model outputs, albeit in a different worksheet, using the same function OutputWorkSheet.Range('A1').value.

The output cells are numerical datatypes, and when changing inputs using the excel app, the values on the outputs are a numerical datatype and output as normal. However, when accessing the output cell using the pywin32 library, the cell always returns -2146826246, indicating a CVErr.

I've also tried other methods of accessing the cell value, such as using worksheet.Cells(1,1).value, with the same -2146826246 error code being thrown.

I'm trying to identify the source of the error, and the only resource found so far talks about it in the context of .NET: link.

Code, although not the full implementation:

import win32com.client 


example_file_location = "path/to/file.xlsx"

process_File = win32com.client.Dispatch("Excel.Application",pythoncom.CoInitialize()) 

process_File.Visible = 0


workBook = process_File.Workbooks.open(example_file_location) 
inputWorkSheet = workBook.Worksheets('Inputs')
outputWorkSheet = workBook.Worksheets('Outputs')
...
#code to give inputs
...

x = outputWorkSheet.Range('A1').value

0

There are 0 answers