Run a macro from an excel file, Python, white spaces on file name problem

277 views
Skip to first unread message

Sebastian Vega

unread,
Dec 2, 2015, 1:24:08 PM12/2/15
to python-excel
Hi I had a problem with run a macro from an excel file.

If the excel file don't have "white spaces" on his name the code runs perfect. But if the file is called like "Test macro.xlsm" the code display error.

For sure i'm missing something

here is the code


from win32com.client import Dispatch

def runmacros():

     mc_n
= 'Test Macro.xlsm!Test_macro.Prueba'
     fl_run
= r'C:\Test\Test Macro.xlsm'

     run_macro
(mc_n, fl_run)
       
     
     
return render_template('main.jade')


def run_macro(macroname, filetorun):
            pythoncom
.CoInitialize ()
            xlApp
= Dispatch('Excel.Application')
            xlWb
= xlApp.Workbooks.Open(filetorun)
            xlSht
= xlWb.WorkSheets(1)
            xlApp
.visible = True
            xlApp
.Application.Run(macroname)
            xlWb
.Saved = 0
            xlWb
.Save()
            xlWb
.Close(SaveChanges=True)
            xlApp
.application.Quit()

How i can avoid this problem?

thanks

Jose Celso Bautista

unread,
Dec 2, 2015, 4:26:34 PM12/2/15
to python...@googlegroups.com
from win32com.client import Dispatch
    

def run_macro(macroname, filetorun):          
            xlApp = Dispatch('Excel.Application')
            xlWb = xlApp.Workbooks.Open(filetorun)
            xlSht = xlWb.WorkSheets(1)
            xlApp.visible = True
            xlApp.Application.Run(macroname)
            xlWb.Saved = 0
            xlWb.Save()
            xlWb.Close(SaveChanges=True)
            xlApp.application.Quit()


mc_n= "'Tipo de Cambio.xlsm'!tipoCambioUSD";
fl_run= 'C:\\test\\Tipo de Cambio.xlsm';
run_macro(mc_n, fl_run);


A mi me funciono con este codigo

--
You received this message because you are subscribed to the Google Groups "python-excel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-excel...@googlegroups.com.
To post to this group, send email to python...@googlegroups.com.
Visit this group at http://groups.google.com/group/python-excel.
For more options, visit https://groups.google.com/d/optout.

Jose Celso Bautista

unread,
Dec 2, 2015, 4:30:25 PM12/2/15
to python-excel
from win32com.client import Dispatch
    

def run_macro(macroname, filetorun):          
            xlApp = Dispatch('Excel.Application')
            xlWb = xlApp.Workbooks.Open(filetorun)
            xlSht = xlWb.WorkSheets(1)
            xlApp.visible = True
            xlApp.Application.Run(macroname)
            xlWb.Saved = 0
            xlWb.Save()
            xlWb.Close(SaveChanges=True)
            xlApp.application.Quit()


mc_n= "'Tipo de Cambio.xlsm'!tipoCambioUSD";
fl_run= 'C:\\App\\AppExcel\\Tipo de Cambio.xlsm';
run_macro(mc_n, fl_run);

Reply all
Reply to author
Forward
0 new messages