473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ 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("Exce l.Application")
ppt = win32com.client .Dispatch("Powe rPoint.Applicat ion")
xl.Visible = 1 #open MS Excel
ppt.Visible = 1 #open MS Powerpoint
xl.Workbooks.Op en('Z:\\project s\\surveys\\SPS S - Generic files\\big
output.xls') #A table for a project
xl.Workbooks.Op en('Z:\\project s\\surveys\\SPS S - Generic
files\\CTP.xla' ) # Stored macro add-in
ppt.Presentatio ns.Open('Z:\\pr ojects\\surveys \\SPSS - Generic
files\\Basic Template.ppt')
xl.Application. ExecuteExcel4ma cro('CTP!CTP.xl a()"[big output.XLS]')

Oct 18 '06 #1
4 7949

mi************* *@tangozebra.co m 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("Exce l.Application")
ppt = win32com.client .Dispatch("Powe rPoint.Applicat ion")
xl.Visible = 1 #open MS Excel
ppt.Visible = 1 #open MS Powerpoint
xl.Workbooks.Op en('Z:\\project s\\surveys\\SPS S - Generic files\\big
output.xls') #A table for a project
xl.Workbooks.Op en('Z:\\project s\\surveys\\SPS S - Generic
files\\CTP.xla' ) # Stored macro add-in
ppt.Presentatio ns.Open('Z:\\pr ojects\\surveys \\SPSS - Generic
files\\Basic Template.ppt')
xl.Application. ExecuteExcel4ma cro('CTP!CTP.xl a()"[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
ExcecuteExcel4M acro 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").Activa te ) 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
ExecuteExcel4ma cro 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.publi c.excel.program ming
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!sh eet1.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\Li b\site-packages\win32c om\client\dynam ic.py", line
258, in _ApplyTypes_
result = self._oleobj_.I nvokeTypes(*(di spid, 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\Li b\site-packages\win32c om\client\dynam ic.py", line
258, in _ApplyTypes_
result = self._oleobj_.I nvokeTypes(*(di spid, 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
2801
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 thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
5
2064
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 similar functionality. I am a big fan of Python as an OO language and use it for many system admin utilities, webdev and even MS Excel AddIn development, but I have found that Perl's Template::Extract is so invaluable to my web scraping utilities...
1
13105
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 specific ranges and enter data into cells, but what I'm looking for at this point is how to set the background color of cells. Does anybody know how to do this with Win32::OLE? If this module maps to Microsoft's API, is there any complete...
3
28527
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 figure it out. Here is my sample code: import win32com.client xl=win32com.client.Dispatch("Excel.Application") xl.Visible=1 wb = xl.Workbooks.Add( )
1
1871
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 executing and giving the proper result when I don't use IF or CASE statements in the SQL command. Because of this I am facing problem... Can anybody tell me how to overcome this.
2
3018
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 Settings\Administrator.SLB_EUROPE\Desktop\norm ops 150607.xls" ' Select Asset & Portfolio Parameter sheet Sheets("Asset & Portfolio Parameter").Select ' Change the number of iterations Range("D7").Select ActiveCell.FormulaR1C1 = "50"
8
1948
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 rather odd behaviors: 1) readline support doesn't work. 2) you have call sys.exit() to exit (neither ctrl-D or ctrl-Z work)
1
1815
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 ~/.ooo3/user/Scripts/python/, but it will not show up in any macro dialog in Ooo 3.0. The provided example macros seem to work: under the "Tools -Macros -> Organize Macros -Python..." menu, there is an entry named "OpenOffice.org Macros", and I can run the...
1
1443
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
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
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 we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.