473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Win32 python and excel macros

Hi Experts,

Looking for a very quick bit on of advice on how to make some python
code run. I'm a newbie to both VBA and Python, so i apologise if this
is very easy but i'm about to tear my hair out after googling for the
last 3 days.

I have written a large python script which inside of it creates an
Excel table, the name of this file and how many objects can change for
each project i run.

I have then written a VBA script which takes the info from Excel and
drops it into a PowerPoint Pres.

Both of these procedures work fine, but i am coming unstuck when i try
to apply the macro, (or .xla) file to the new tables autmatically. Can
anyone give me any guidance on this?

The macro is called sub is CTP and the add-in file is CTP.XLA

Below is the code i've managed to 'Stick' together

Mike

import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
ppt = win32com.client.Dispatch("PowerPoint.Application")
xl.Visible = 1 #open MS Excel
ppt.Visible = 1 #open MS Powerpoint
xl.Workbooks.Open('Z:\\projects\\surveys\\SPSS - Generic files\\big
output.xls') #A table for a project
xl.Workbooks.Open('Z:\\projects\\surveys\\SPSS - Generic
files\\CTP.xla') # Stored macro add-in
ppt.Presentations.Open('Z:\\projects\\surveys\\SPS S - Generic
files\\Basic Template.ppt')
xl.Application.ExecuteExcel4macro('CTP!CTP.xla()"[big output.XLS]')

Oct 18 '06 #1
4 7920

mi**************@tangozebra.com wrote:
Hi Experts,

Looking for a very quick bit on of advice on how to make some python
code run. I'm a newbie to both VBA and Python, so i apologise if this
is very easy but i'm about to tear my hair out after googling for the
last 3 days.

I have written a large python script which inside of it creates an
Excel table, the name of this file and how many objects can change for
each project i run.

I have then written a VBA script which takes the info from Excel and
drops it into a PowerPoint Pres.

Both of these procedures work fine, but i am coming unstuck when i try
to apply the macro, (or .xla) file to the new tables autmatically. Can
anyone give me any guidance on this?

The macro is called sub is CTP and the add-in file is CTP.XLA

Below is the code i've managed to 'Stick' together

Mike

import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
ppt = win32com.client.Dispatch("PowerPoint.Application")
xl.Visible = 1 #open MS Excel
ppt.Visible = 1 #open MS Powerpoint
xl.Workbooks.Open('Z:\\projects\\surveys\\SPSS - Generic files\\big
output.xls') #A table for a project
xl.Workbooks.Open('Z:\\projects\\surveys\\SPSS - Generic
files\\CTP.xla') # Stored macro add-in
ppt.Presentations.Open('Z:\\projects\\surveys\\SPS S - Generic
files\\Basic Template.ppt')
xl.Application.ExecuteExcel4macro('CTP!CTP.xla()"[big output.XLS]')
It doesn't really make sense to apply a *file* to a *file* - you apply
a sub or function in that file to a range in the other file (I'm
assuming that your table is stored as a range of cells). What
ExcecuteExcel4Macro is expecting as input is a string along the lines
of
'CTP!MacroName(Workbooks("big output").Range("A1:C100"))'
(experiment with using "" instead of " since VBA requires embedded " to
be escaped by "" - but since you are writing this in Python it might
not be necessary). Maybe experiment with writing a VBA macro in Excel
which can successfuly launch the macro you need and then translate the
appropriate snippet to your python script. Also - are you sure that the
add-in macro is an old-style Excel4 macro? That would make it about 10
years old or deliberately retro. If not - the run method might be more
appropriate.

You should probably open the workbooks in such a way that big
output.xls is the active workbook (and not ctp.xla) since most add-ins
assume that the calling workbook is the active workbook (although - I
don't know how an old-style pre-VBA Excel4 macro handled things). Thus
you would probably want to open big output.xls last (or use
xl.Application.Workbooks("big output").Activate ) to make sure that it
is the active workbook. Also - do you even have to open ctp.xla
explicitly? If it is an installed add-in then that line might be
redundant.

A final potential problem is that big output.xls might require a
reference to ctp.xla. This sometimes happens when you try to invoke
add-in code from another VBA project - but I would think that the
ExecuteExcel4macro would bypass that.

I can't comment on the python part of the equation - I am a complete
newbie there.

You might consider reposting this in microsoft.public.excel.programming
since many of the regular posters there know a lot about automating
Excel from scripting languages. They could at least help you with the
VBA side of the equation.

I hope that my random thoughts don't misguide you too much.

-John Coleman

Oct 18 '06 #2
Thanks for your advice on this matter,

I'm actually using Excel 2003!! so it shows how much i know!

i did manage to get the prog to run with the line
xl.Application.Run("CTP.xla!sheet1.CTP")

but it didn't do anything... i'm guessing it is along the lines of wht
you were saying earlier about big output being the active worksheet.

I'll give that a go now

Thanks for the advice

Mike

Oct 18 '06 #3
After just running trying that update it hits the macro perfectly but
hten i get an error message after i type in a couple of values.. as per
below

Traceback (most recent call last):
File "<string>", line 148, in ?
File "<COMObject <unknown>>", line 14, in Run
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line
258, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,
argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None,
None, None, 0, -2146827284), None)
I know you said you didn't know much about python, so if any other
experts outthere can give me a clue.. i'll be very appreciative

Mike

Oct 18 '06 #4

Mike P wrote:
After just running trying that update it hits the macro perfectly but
hten i get an error message after i type in a couple of values.. as per
below

Traceback (most recent call last):
File "<string>", line 148, in ?
File "<COMObject <unknown>>", line 14, in Run
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line
258, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,
argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None,
None, None, 0, -2146827284), None)
I know you said you didn't know much about python, so if any other
experts outthere can give me a clue.. i'll be very appreciative

Mike
Mike,

Maybe you can use a little error handling in the VBA code to see if the
error is in the VBA or in the Python (or both)

At the top of the sub that you call put the line

On Error GoTo err_handler

and at the bottom of the sub (right before the end sub) put:

err_handler:
If Err.Number 0 Then
MsgBox "Error " & Err.Number & ": " & Err.Description
End If

This will also give you some idea of what the error is from VBA's
perspective (although the error descriptions are not always very
informative).

HTH

-John Coleman

Oct 18 '06 #5

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

Similar topics

37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
5
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had...
1
by: M. David Allen | last post by:
Hello, I've been using ActiveState's ActivePerl to generate Excel spreadsheets using the Win32::OLE module. The rudimentary examples that are out there on the web show the way to set up...
3
by: zxo102 | last post by:
Hi there, I need your help for python <--> excel. I want to paste selected cells (range) to different location on the same sheet in Excel through python. I have tried it for a while but could not...
1
by: Praveen James | last post by:
Hi, when I execute a T-SQL command (which have IF Condition or CASE WHEN THEN statement) using excel macros I am getting a error "Item Not found in the collection.....". But the vba excel macro is...
2
by: modularmix | last post by:
Does anyone know the code for running the Excel Macro for two different spreadsheets in parallel. Here is the code that works for them sequentially. Workbooks.Open Filename:="C:\Documents and...
8
by: Grant Edwards | last post by:
When I ssh into a windows machine (running Cygwin sshd), I can invoke python at the shell prompt (you have to use -i option and I don't really understand why). Once it's started there are couple...
1
by: Lawrence D'Oliveiro | last post by:
Has anyone been able to run user-defined Python macros in OpenOffice.org 3.0? I had one in ~/.ooo-2.0/user/Scripts/python/try.py which did work under Ooo2.x. So I put the same thing in...
1
by: pnalla | last post by:
hi how to create a word documetn using Excel macros.. i have some code but it crating a word document but it isnot close properly whats the proble. please suggest me. thanks prasad
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.