Use macros in Excel via win32com

C

chris

I'm creating an excel document dynamically from scratch using Python
and the win32com module. All is well, but now I need to add a macro to
the spreadsheet and run it (to enable some sorting features in the
spreadsheet). I think I know how to run a macro once it's installed
(using the Run method of the excel application object...I think), but I
can't figure out how to "install" the VBA macro code into the
spreadsheet to begin with from my Python script.
Any tips appreciated.

Thanks,
Chris
 
B

beliavsky

chris said:
I'm creating an excel document dynamically from scratch using Python
and the win32com module. All is well, but now I need to add a macro to
the spreadsheet and run it (to enable some sorting features in the
spreadsheet). I think I know how to run a macro once it's installed
(using the Run method of the excel application object...I think), but I
can't figure out how to "install" the VBA macro code into the
spreadsheet to begin with from my Python script.
Any tips appreciated.

Here is a manual solution for Excel 2003. At the bottom of the spreadsheet,
right-click on one of the tabs (default names sheet1 sheet2 sheet3). Click
"view code" on the menu, which should bring up a Microsoft Visual Basic editor.
Select Module from the Insert menu at the top, and paste the VBA code there.
 
C

chris

Thanks, but the problem is that I need to create the entire Excel
document from scratch, dynamically, via the Python script. I want the
script to add the macro code.

-Chris
 
G

Gregor Horvath

chris said:
I'm creating an excel document dynamically from scratch using Python
and the win32com module. All is well, but now I need to add a macro to
the spreadsheet and run it (to enable some sorting features in the
spreadsheet). I think I know how to run a macro once it's installed
(using the Run method of the excel application object...I think), but I
can't figure out how to "install" the VBA macro code into the
spreadsheet to begin with from my Python script.
Any tips appreciated.

Make a xla in Excel including your VBA macro first and then create your
spreadshet from python using that xla as template.
 
H

Harald Massa

chris,
I'm creating an excel document dynamically from scratch using Python
and the win32com module. All is well, but now I need to add a macro to
the spreadsheet and run it (to enable some sorting features in the
spreadsheet).

I recommend to create the excel document not from scratch but from a
template.

I learned from experience that it is speadier to have all the static parts
allready prepared - and especially diagrams and macros are MUCH easier to
handle if allready in the programm

Harald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top