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 an XYLine Graph. I got some sample code and was trying it but seems like I get an error when I try to Name the chart title, enable the legend, or name the Data Series.
The error is the following when trying to name the chart title:
OLE exception from "Microsoft Office Excel": Unable to set the HasTitle property of the Chart class Win32::OLE(0.1707) error 0x80020009: "Exception occurred" in PROPERTYPUT "HasTitle" at Processor.pl line 1044

Line 1044 is the following in the code below:
$mcr_crtime_chart->{HasTitle} = 1; $mcr_crtime_chart->ChartTitle->{Text} = "MCRByURN-CreateTime";

The error is the following when trying to enable the legend:
OLE exception from "Microsoft Office Excel": Unable to set the Position property of the Legend class Win32::OLE(0.1707) error 0x80020009: "Exception occurred" in PROPERTYPUT "Position" at Processor.pl line 1043

Line 1043 is the following from the code below:
$mcr_crtime_chart->{HasLegend} = 1; $mcr_crtime_chart->Legend->{Position} = xlRight;

The error is the following when trying to name a series:

OLE exception from "Microsoft Office Excel": Unable to set the Name property of the Series class Win32::OLE(0.1707) error 0x80020009: "Exception occurred" in PROPERTYPUT "Name" at Processor.pl line 1056


Line 1056 is the following line of code $mcr_crtime_chart->SeriesCollection($p)->{Name} = "Series $p"; in the code below (Last Line in the for loop)
The Code I am using is trying to graph N number of series worth of data and therefore has the FOR LOOP. The code 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_crtime_chart = $Excel->Charts->Add({After => $mcrsa_crtab_she +et}); $mcr_crtime_chart->{Name} = "CreateTime_Graph"; $mcr_crtime_chart->{ChartType} = xlXYScatterLines; $mcr_crtime_chart->{HasLegend} = 1; # $mcr_crtime_chart->Legend->{Position} = xlRight; # $mcr_crtime_chart->{HasTitle} = 1; # $mcr_crtime_chart->ChartTitle->{Text} = "MCRByURN-CreateTime"; $x_range = $mcrsa_crtab_sheet->Range("B2:B69"); $mcr_crtime_chart->SetSourceData($mcrsa_crtab_sheet->Range("A2:D69 +"),xlColumns); $mcr_crtime_chart->SeriesCollection->LetProperty('XValues',$x_rang +e); for ($p = 2; $p < $ct_max_column; $p++) { $y_range[$p] = $mcrsa_crtab_sheet->Range("$LetterByNumber{$p}2:$ +LetterByNumber{$p}$ct_row"); $mcr_crtime_chart->SeriesCollection($p)->LetProperty('YValues',$ +y_range[$p]); $mcr_crtime_chart->SeriesCollection($p)->{Name} = "Series $p"; } my $Xaxes = $mcr_crtime_chart->Axes(xlCategory,xlPrimary); $Xaxes->{HasTitle} = 1; $Xaxes->{AxisTitle}->{Characters}->{Text} = "Create Time (sec)"; my $Yaxes = $mcr_crtime_chart->Axes(xlValue,xlPrimary); $Yaxes->{HasTitle} = 1; $Yaxes->{AxisTitle}->{Characters}->{Text} = "MCR (%)";
If anyone has any suggestions they would be greatly appreciated. Thanks in advance!

Replies are listed 'Best First'.
Re: Problem Graphing to Excel with Win32::OLE
by marto (Cardinal) on Jun 05, 2008 at 19:56 UTC
    I am not sure, but I think

    $mcr_crtime_chart->Legend->{Position} = xlRight;

    should be

    $mcr_crtime_chart->Legend->{Position} = xlLegendPositionRight;

    You may want to search msdn, check out XlLegendPosition Enumeration:
    Specifies the position of the legend on a chart. Name Value Description xlLegendPositionBottom -4107 Below the chart. xlLegendPositionCorner 2 In the upper right-hand corner of the c +hart border. xlLegendPositionCustom -4161 A custom position. xlLegendPositionLeft -4131 Left of the chart. xlLegendPositionRight -4152 Right of the chart.

    I can't test this right now, as I do not have access to a Windows system (I am not at work). Give it a shot and let us know how you get on.

    Hope this helps

    Martin
      Similarly, I believe HasTitle and HasLegend need to be set to one of the various "True" options, perhaps msoTrue or the variant True. (MSDN documentation does not describe HasTitle.)
        Ah, nice catch, perhaps

        $mcr_crtime_chart->ChartTitle->{Text} = "MCRByURN-CreateTime";

        Should be:

        $mcr_crtime_chart->ChartTitle->{Characters}->{Text} = "MCRByURN-CreateTime";

        Sometimes if find it difficult to find things via the msdn search, and I have never been a huge fan of the Microsoft documentation.

        It may also be worth checking out the URLs I link to in this node.

        Martin
        If you notice the code I place the
        $mcr_crtime_chart->{HasLegend} = 1;
        and the as well
        $mcr_crtime_chart->{HasTitle} = 1;

        but neither seem to work when I uncomment those lines of code and if I try to name the chart $mcr_crtime_chart->ChartTitle->{Text} = "CreateTime"; it fails.

        I tried the XlLegendPosition Enumeration and it doesn't seem to work either.
Re: Problem Graphing to Excel with Win32::OLE
by nbartusi (Scribe) on Jun 06, 2008 at 14:59 UTC
    I often use the ChartWizard to add a title and legend.
    my $Chart2 = $Book->Charts->Add; $Chart2->ChartWizard($Range2,xl3DPie,6,xlColumns,1,0,1,"Error Perc +entages"); #uses the first column to name each series
    MS Doc

    And I'll second the advice to start with a small working script and slowly add to it.