ralife has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am using Active Perl 5.8.0 Build 819 and have created a program which will parse data out of a MS Access DB. I am then taking the data parsed and output it formated in Excel using the Win32::OLE. The problem I am having is when I try to generate a Line Graph. I got some sample code and was trying it but seems like I get an error when running it. The error is the following:

Can't call method "SetSourceData" without a package or object reference at Processor.pl line 893.

The Code I am using is as follows:
use OLE; use Win32::OLE qw(in with); use Win32::OLE::Const "Microsoft Excel"; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Excel = Win32::OLE->GetActiveObject('Excel.Application')||Win32::OLE- +>new('Excel.Application','Quit'); $Excel->{DisplayAlerts}=0; $Excel->{SheetsInNewWorkbook} = 16; $Book = $Excel->Workbooks->Add; $mcr_crtab_chart_sheet_name = "CreateTime_Chart"; $chart_sheet = $Book->Worksheets(16); $chart_sheet->{Name} = $mcr_crtab_chart_sheet_name; $mcr_crtime_chart = $Excel->Charts-Add; $mcr_crtime_chart->{ChartType} = xlLineMarkers; # or 65 $mcr_crtime_chart->SetSourceData({Source => $mcrsa_crtab_sheet->Rang +e("B1:F12"),PlotBy => xlColumns}); $range_1 = $mcrsa_crtab_sheet->Range("C2:C$row"); $range_2 = $mcrsa_crtab_sheet->Range("D2:D$row"); $mcr_crtime_chart->SeriesCollection(1)->LetProperty('XValues',$range +_1); $mcr_crtime_chart->SeriesCollection(1)->{Name} = "Series1"; $mcr_crtime_chart->SeriesCollection(2)->LetProperty('XValues',$range +_2); $mcr_crtime_chart->SeriesCollection(2)->{Name} = "Series2"; $mcr_crtime_chart->SeriesCollection(1)->ApplyDataLabels( { AutoText +=> 1,ShowValue => 1 } ); $mcr_crtime_chart->SeriesCollection(2)->ApplyDataLabels( { AutoText +=> 1,ShowValue => 1 } ); $mcr_crtime_chart->{HasLegend} = 1; $mcr_crtime_chart->Legend->{Position} = xlRight; $mcr_crtime_chart->{HasTitle} = 1; $mcr_crtime_chart->ChartTitle->{Text} = "CreateTime"; $mcr_crtime_chart->Axes(xlCategory,xlPrimary)->{HasTitle} = 1; $mcr_crtime_chart->Axes(xlCategory,xlPrimary)->AxisTitle->{Text} = " +Create Time (sec)"; $mcr_crtime_chart->Location({Where=> xlLocationAsObject, Name=> $mcr +_crtab_chart_sheet_name}); $Excel->{ActiveChart}->{Parent}->{top} = 10; $Excel->{ActiveChart}->{Parent}->{left} = 10;
If anyone has any suggestions they would be greatly appreciated. Thanks in advance!

Replies are listed 'Best First'.
Re: Need Help with Creating Excel Line Chart using Win32:OLE
by nbartusi (Scribe) on May 21, 2008 at 17:36 UTC
    That type of error usually means you don't have an object where you think. In this case it says $mcr_crtime_chart is not a valid object that you can call SetSourceData on. I've found this is usually happens when creating the object fails. So the real problem is with:
    $mcr_crtime_chart = $Excel->Charts-Add;

    Change it to:
    $mcr_crtime_chart = $Book->Charts-Add;
      Thanks! It turns out its the little things that makes life difficult.

      I just forgot to put the ">" in the "->"

      $mcr_crtime_chart = $Book->Charts-Add;
      Thanks for the help!