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?

Dominique
  • 16,450
  • 15
  • 56
  • 112
bozo
  • 11
  • 1

1 Answers1

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.

S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • Thank you for input! Unfortunately it keeps crashing with xlsm file. I even added some code via editor to ensure that codemodule exists - still no luck. – bozo Feb 13 '19 at 12:34