Win32 python and excel macros

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael.pearmain@tangozebra.com

    #1

    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]')

  • John Coleman

    #2
    Re: Win32 python and excel macros


    michael.pearmai n@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

    Comment

    • Mike P

      #3
      Re: Win32 python and excel macros

      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

      Comment

      • Mike P

        #4
        Re: Win32 python and excel macros

        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

        Comment

        • John Coleman

          #5
          Re: Win32 python and excel macros


          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

          Comment

          Working...