locked
excel copy method throw exception RRS feed

  • Question

  • I have an excel , there are some charts and would like to copy to word .

    $workbook = $excel.workbooks.open($excelFiles)
            $worksheet = $workbook.worksheets.item('LPAR')
            $charts = $worksheet.ChartObjects()
            $chart = $charts.Item('2')
            $a = $chart.copy()               <<<<< this line throw exception

    Exception calling "Copy" with "0" argument(s): "Exception from HRESULT: 0x800A03EC"
    At line:1 char:12
    + $chart.copy <<<< ()
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ComMethodTargetInvocation

    There are  3 charts in the sheet, I can do  $chart = $charts.Item('1') .

    but $chart = $charts.Item('2') . $chart = $charts.Item('3') . get the same error. 

    any idea . ??

    it's excel and word 2016


    Tuesday, January 23, 2018 1:47 AM

All replies

  • $chart = $charts.Item('2')

    Item is an index number and not a string.

    $workbook = $excel.workbooks.open($excelFiles)
    $workbook.worksheets.['LPAR'].ChartObjects('Chart 1').Copy()


    \_(ツ)_/



    • Edited by jrv Tuesday, January 23, 2018 2:30 AM
    Tuesday, January 23, 2018 2:23 AM
  • We can also just do this:

    $workbooks.Worksheets['LPAR'].Shapes['Chart 1'].Copy()

    Replace "Chart 1" with the name of your chart.


    \_(ツ)_/



    Tuesday, January 23, 2018 2:37 AM