(Excel) VBA Crashes wihile accessing CodeModule

1

I'm trying to insert some code to VBA codemodule and excel crashes while reading CodeModule property. Here's an example code in python:

excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
workbook = excel.Workbooks.Open("C:\\test\\test1.xlsx")
excel.VBE.MainWindow.Visible = False
sheet = workbook.Worksheets(1) 
vbp = workbook.VBProject
comp = vbp.VBComponents(workbook.CodeName)
cm = comp.CodeModule   #     <--------------- !!! Excel crashes here
curLine = cm.CreateEventProc("Open", "Workbook")
cm.InsertLines(curLine + 1, "some text")

Faulting application name: EXCEL.EXE, version: 16.0.11231.20174, timestamp: 0x5c5e3867

Faulting module name: combase.dll, version: 10.0.14393.2791, timestamp: 0x5c5a43d3

Exception code: 0xc0000005

OS: Windows Server 2016 Standard, up to date

This is not a pywin32 specific problem, I've tested this. Also I've tried x86 and x64 office versions, both are crashing. What's interesting is that excel com object remains alive and I am still to able to manipulate it and save the file. But VBProject is empty.

Any ideas where to dig?

python
excel
vba
pythoncom
asked on Stack Overflow Feb 13, 2019 by bozo • edited Feb 13, 2019 by Dominique

1 Answer

0

The file is an xlsx so it won't have a VBProject. Admittedly, it ought to throw an error instead of crashing. You need a file type of xlsm.

answered on Stack Overflow Feb 13, 2019 by S Meaden

User contributions licensed under CC BY-SA 3.0