Adding a Trendline using Microsoft.Office.Interop.Graph (PIA)

G

Guest

We are writing C# code to manipulate PowerPoint presentations. In so doing,
we are having to use the Primary Interop Assemblies (PIA) for Office (in this
case, Interop.Microsoft.Office.Core, Microsoft.Office.Interop.Graph, and
Microsoft.Office.Interop.PowerPoint). A road-block we've encountered is
adding a Trendline to a chart inside a slide via code. We can reference
existing Trendlines but cannot seem to create them. For example (minus all
the namespace prefixes for the types):

Graph.Chart newChart1;
newChart1 = (Graph.Chart)presentation.Slides[1].Shapes.AddOLEObject([long
parameter list]).OLEFormat.Object;

/*** Assume data has now been inserted into the datasheet cells which would
establish the collection of Series ***/

Graph.Series series1 = (Graph.Series)newChart1.SeriesCollection(1);

/*** The line below will throw a compilation error because the only exposed
"Trendlines" property or method through the Graph.Series object is method -
object Trendlines(object index) ***/
Graph.Trendlines trendlinesCollection1 = series1.Trendlines;

/*** The line below will throw an error ("Trendlines method of Series class
failed...") because no Trendlines exist against the series yet to be able to
reference them by the index 1...n ***/
Graph.Trendline trendline1 = series1.Trendlines(1);

/*** end of code ***/

When looking in the Object Browser at the Microsoft.Office.Interop.Graph
reference there is an apparent Add( ) function from the Graph.Trendlines
(collection) object but there doesn't appear to be a way to get a reference
to the Trendlines collection. Has anyone ever solved this, or is the
Trendlines collection just not exposed in the PIA to get it from the Series
object.

P.S. - I am aware that in VBA, the following is possible:

With newChart1.SeriesCollection(1).Trendlines
.Add xlLinear
End With

But, as mentioned above, it will failed to compile in C#.

Any Help?!
 
S

Shyam Pillai

Leeder,
I've experienced this while writing code in VB.Net. Haven't found a way
around. It's just missing functionality.
 
G

Guest

darilek,
It worked! Your code works in the Graph library as well! From what I can
tell, we are now on the right track. Now to get the trendline to behave the
way we want it to - that's another task. Thank you very much! You saved us
much time in trying to develop a work-around. :)


darilek said:
To add new trendline into series object You can try fill parameter in
Trendlines() method as System.Reflection.Missing.Value. I've used it
with Excel interop:

Excel.Series excelSeries;
Excel.Trendline excelTrendLine;
object missing = System.Reflection.Missing.Value;

excelSeries = excelApp.ActiveChart.SeriesCollection(1);
excelTrendLine =
((Excel.Trendlines)excelSeries.Trendlines(missing)).Add( ... );

enjoy :)


We are writing C# code to manipulate PowerPoint presentations. In so
doing,
we are having to use the Primary Interop Assemblies (PIA) for Office
(in this
case, Interop.Microsoft.Office.Core, Microsoft.Office.Interop.Graph,
and
Microsoft.Office.Interop.PowerPoint). A road-block we've encountered
is
adding a Trendline to a chart inside a slide via code. We can
reference
existing Trendlines but cannot seem to create them. For example (minus
all
the namespace prefixes for the types):

Graph.Chart newChart1;
newChart1 =
(Graph.Chart)presentation.Slides[1].Shapes.AddOLEObject([long
parameter list]).OLEFormat.Object;

/*** Assume data has now been inserted into the datasheet cells which
would
establish the collection of Series ***/

Graph.Series series1 = (Graph.Series)newChart1.SeriesCollection(1);

/*** The line below will throw a compilation error because the only
exposed
"Trendlines" property or method through the Graph.Series object is
method -
object Trendlines(object index) ***/
Graph.Trendlines trendlinesCollection1 = series1.Trendlines;

/*** The line below will throw an error ("Trendlines method of Series
class
failed...") because no Trendlines exist against the series yet to be
able to
reference them by the index 1...n ***/
Graph.Trendline trendline1 = series1.Trendlines(1);

/*** end of code ***/

When looking in the Object Browser at the
Microsoft.Office.Interop.Graph
reference there is an apparent Add( ) function from the
Graph.Trendlines
(collection) object but there doesn't appear to be a way to get a
reference
to the Trendlines collection. Has anyone ever solved this, or is the
Trendlines collection just not exposed in the PIA to get it from the
Series
object.

P.S. - I am aware that in VBA, the following is possible:

With newChart1.SeriesCollection(1).Trendlines
.Add xlLinear
End With

But, as mentioned above, it will failed to compile in C#.

Any Help?!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top