Python crash when trying to generate an Excel worksheet with VBA macros

dan_roman dan_roman_sb at yahoo.com
Wed Oct 4 06:53:47 EDT 2006


Hi,
I developed a script with a nice interface in Tkinter that allows me to
edit some formulas and to generate an Excel worksheet with VBA macros
within it. The script runs perfectlly in Office 2000, but in Office
2003 crash at line: "wbc = workbook.VBProject.VBComponents.Add(1)"
Please help me :-(

the code of the module that crash is (only in Excel 2003, in 2000 not):

import os
import string
from win32com.client import Dispatch, constants

str_code="""
Dim nrfunc As Integer
Dim cursor As Integer
Dim i As Integer
Dim j As Integer



Sub Fill()


'Aflu numaru de functii din XL
i = 1
..................
"""
def createExcelReport(projectName,templateName,saveToPath):
	# acquire application object, which may start application
	application = Dispatch("Excel.Application")

	# create new file ('Workbook' in Excel-vocabulary) using the specified
template
	workbook = application.Workbooks.Add("Template1.xls")

	# store default worksheet object so we can delete it later
	defaultWorksheet = workbook.Worksheets(1)

	worksheet1 = workbook.Worksheets(1)
	worksheet2 = workbook.Worksheets(2)
	worksheet3 = workbook.Worksheets(3)

---->>>>>	wbc = workbook.VBProject.VBComponents.Add(1) <<<<------ here
is the problem

	wbc.Name="Module1"

	wbc.CodeModule.AddFromString(str_code)

	path=saveToPath+"\\"+projectName+"_"+templateName+".xls"

	workbook.SaveAs(path)

	worksheet1 = workbook.Worksheets(1)

	# make stuff visible now.
	worksheet1.Activate()
	application.Visible = True




More information about the Python-list mailing list