Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32:OLE Excel Graph Title Problem 1

Status
Not open for further replies.

cmpeng

Programmer
Aug 31, 2005
7
0
0
CA
Hello,

I'm currently experiencing some problems displaying an Excel graph title. I have two functions, both open an Excel application, enter some data, and generate a graph for the user. Everything works fine except for the graph title in the second function.

Here's the chart code for the working function.

sub Res_stdd_Click
{
my $chart;

...
...

# Create chart
$chart = $xlBook->Charts->Add;
$chart->SetSourceData($rng, 2);
$chart->{ChartType} = 58; # Bar graph
$chart->ChartTitle->{Text} = "Std. Dev. HSDL/GWY/SSMS";
$chart->Location(1, "CHART");

...
...

return;

}

Below is the code in which everything works except the chart title does not show up.

sub Res_wave_Click
{
my $chart;

...
...

# Create chart
$chart = $xlBook->Charts->Add;
$chart->SetSourceData($rng, 2);
$chart->{ChartType} = 73; # line graph
$chart->ChartTitle->{Text} = "HSDL/GWY/SSMS Waveforms";
$chart->Location(1, "CHART");

...
...

return;
}

As you can see the code is almost completely identical except for the chart type, which is necessary. In fact, the code is directly from the Microsoft's example on using PERL with Excel. I am aware of a bug in $chart->Location that causes $chart to become invalid so it's called last. I also tried $chart->HasTitle = 1, but it throws an error detailing a non-lvalue reference. Furthermore, I have also tried accessing the title by $chart->ChartTitle->Characters->{Text}, but this throws "Can't use an undefined value as a Hash reference." error.

Anyone have any ideas on why I am experiencing this behavior? Or am I just simple using the wrong chart type?

Thanks in advice!

------
Matthew King
MaKing@nbpower.com
Computer Design & System Performance,
Pt. Lepreau Nuclear Generating Station,
NB Power,
Maces Bay, N.B.,
 
Hello,

Found the solution and figured I would share it.

Before discussing the solution, I want to point out how this title display problem doesn't appear in the first function which is a complete mystery to me.

OLD CODE:
sub Res_wave_Click
{
my $chart;

...
...

# Create chart
$chart = $xlBook->Charts->Add;
$chart->SetSourceData($rng, 2);
$chart->{ChartType} = 73; # line graph
$chart->ChartTitle->{Text} = "HSDL/GWY/SSMS Waveforms";
$chart->Location(1, "CHART");

...
...

return;
}

New Code
sub Res_wave_Click
{
my $chart;

...
...

# Create chart
$chart = $xlBook->Charts->Add;
$chart->SetSourceData($rng, 2);
$chart->{ChartType} = 73; # line graph

# Move chart to new sheet, loses object!!
$chart->Location(1, "CHART");

# Grab object agin
$chart = $xlApp->ActiveChart;

# Assign title
with($chart,HasTitle =>1);
$chart->ChartTitle->{Text} = "HSDL/GWY/SSMS Waveforms";

...
...

return;
}

Although $chart-Location does lose the object, I have no idea why the old code doesn't work since the title is assigned before the object is lost. Hope this saves some people time in the future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top