[python-win32] Runing an Excel macro from python

thor vollset thor at tordivel.no
Sun Jun 1 20:34:29 EDT 2003


this will run an excel macro from python

from win32com.client import Dispatch

def RunExcelMacro(name):
    myExcel = Dispatch('Excel.Application')
    myExcel.Visible = 0
    myExcel.Workbooks.Add('c:\Termoetiketter1_2.xls')
    myExcel.Run(name)
    myExcel.DisplayAlerts = 0
    myExcel.Quit()

all the best

Thor
Vollset --------------------------------------------------------------------
-------------- 
TORDIVEL AS Storgata 20 0184 Oslo Norway
Tel: +47 23 15 87 00 Fax: +47 23 15 87 01
E-mail: thor at tordivel.no Web: http://www.tordivel.no
----------------------------------------------------------------------------
------ 
Scorpion Vision Software - Eliminates your weakest link
----------------------------------------------------------------------------
------ 

----- Original Message ----- 
From: "Python" <python at telefonica.net>
To: <python-win32 at python.org>
Sent: Sunday, June 01, 2003 1:32 PM
Subject: [python-win32] Runing an Excel macro from python


> Hi everybody, I´m newbie with python and my english is not very fluent so
i´ll try to explain my problem as better as i can::
>
> I want to open an excel  document and save it as .CSV extension. I have a
macro for that job, saved into the personal macros book called
"PRESONAL.xls"
> If i do double click on PERSOANL.xls, excel is visible and I can execute
the macro (for save as .CSV).
> The problem begins with python :( i do this:
>
> import win32com.client
> import os
> import tempfile
> xl= win32com.client.Dispatch("Excel.Application")
> xl.Visible=1
> xl.Workbooks.Add('D:\PERSONAL.xls')
>
> and then excel is open but hanged on. The error message on the Python
interpreter says:
>
> File
"C:\PYTHON22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
>     exec codeObject in __main__.__dict__
> File "D:\pruebas-macro.py", line 6, in ?
>     xl.Workbooks.Add('D:\PERSONAL.xls')
> File
"C:\PYTHON22\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-00000
0000046x0x1x2\Workbooks.py", line 30, in Add
>     ret = self._oleobj_.InvokeTypes(181, LCID, 1, (9, 0), ((12,
17),),Template)
> com_error: (-2147417851, 'Server launches an exception.', None, None)
>
> What i´m doing wrong? maybe i have to call to "pythoncom.CreateGuid()" or
use IDispatch instead of Dispatch?
>  I have been searching for a full example script in python calling excel
but i only found a few example lines (not a full program). I would very
pleased if anybody could tell me where I can  found a full example program.
>
> Thanks to everybody.
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
>
>





More information about the Python-win32 mailing list