472,338 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

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

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,saveToP ath):
# 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

Oct 4 '06 #1
3 3797
hg
dan_roman wrote:
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,saveToP ath):
# 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
Crash as in Office or as a Python exception ?

Oct 4 '06 #2
dan_roman wrote:
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 Excel on the box you're testing this, have you set it to trust
programmatic access to the VB Project? If you haven't, I wouldn't be
surprised if that line would likely raise an exception. I'm pretty
sure Excel's security model has changed a bit between 2000 and 2003, so
perhaps this is what is biting you.

Oct 4 '06 #3

Apologies in advance to anyone who gets this twice, but I posted this
using Thunderbird over 11 hours ago and it's not showing up ...
=================================
On 4/10/2006 8:53 PM, dan_roman wrote:
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 :-(
Please consider helping yourself:
(1) Show what error message you got
(2) Provide the bare minimal code that will reproduce the problem.

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

import os
import string
### not used
from win32com.client import Dispatch, constants
### do yourself a favour, use makepy
>
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,saveToP ath):
# 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")
Where do you keep this file? I had to supply a full path, otherwise I
was getting a file-not-found error.
>
# store default worksheet object so we can delete it later
defaultWorksheet = workbook.Worksheets(1)
### not needed
>
worksheet1 = workbook.Worksheets(1)
worksheet2 = workbook.Worksheets(2)
worksheet3 = workbook.Worksheets(3)
### not needed
>
---->>>> 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
and no code to call it.

I wrote some code to call it, created an empty default spreadsheet
called c:\junk\Template1.xls, ran it, got file-not-found, changed code
to have r"c:\junk\Template1.xls" and ran it again.

Sure enough, it crashed on the VBComponents.Add(1) thing. Here is the
result:

C:\junk>\python24\python dan_roman.py
Traceback (most recent call last):
File "dan_roman.py", line 50, in ?
saveToPath=r'c:\junk',
File "dan_roman.py", line 31, in createExcelReport
wbc = workbook.VBProject.VBComponents.Add(1)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 496, in
__getattr__
if d is not None: return getattr(d, attr)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 455, in
__getattr__
return self._ApplyTypes_(*args)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py",
line 446, in
_ApplyTypes_
return self._get_good_object_(
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0,
'Microsoft Office
Excel', 'Programmatic access to Visual Basic Project is not
trusted\n', 'C:\\Pr
ogram Files\\Microsoft Office\\OFFICE11\\1033\\xlmain11.chm', 0,
-2146827284), N
one)

====
Interesting: "Programmatic access to Visual Basic Project is not
trusted"

So I opened up Template1.xls and did Macros Security Trusted
Publishers and ticked "Trust access to Visual Basic Project" and saved
the file and exited Excel and ensured there were no ghost Excel.exe
processes lurking.

[Confessions (1) I'm not in the habit of mucking about with VBA, so I
didn't get this right first time (2) William Ferschlugginer Gates was
mentioned once or twice, not in a complimentary fashion :-)]

Now the code appears to work. It creates a file, in which your VBA guff
can be seen by opening it with Excel and entering the VBA editor.

So, the $64,000 question is: "What error message did *you* get?"

My versions:
Excel 2003 (11.8033.8036) SP2
pywin32 build 209
Python 2.4.3

HTH,
John

Oct 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn....
4
by: adrian | last post by:
hello! i am using visual studio 2000 and am trying to insert a formúla into an excel cell. the code goes like this: with excel...
6
by: Anand | last post by:
Hello, Can I get some help on how to read the excel files using python? from win32com.client import Dispatch xlApp =...
1
by: Muskito | last post by:
HELP!!! Hello All, I'm using VB.net 2003 and trying to update data in Excel worksheet. The program selects data from the excel, updates...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no...
1
by: Girish | last post by:
Hi, I want to embed a txt document into an excel using python. Here is my code, but i get an error message...
0
by: Smish | last post by:
I have created a class library which has user defined functions for excel. One of functions need reference to current excel so as to insert value to...
0
by: serhio | last post by:
Hello, I have a problem with Microsoft.Office.Interop.Excel.Range.Group() method. The problem is that I can call this method only 7 times,...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.