PowerPoint Interop - How to access Chart out of a shape? - charts

I am working in an office automation tool to process PowerPoint documents, where I am facing an issue in formatting Chart.
I am using the following assemblies for formatting the Presentation
Microsoft.Office.Interop.PowerPoint Version: 12.0.0.0
Microsoft.Office.Interop.Graph Version: 12.0.0.0
Requirement is to apply some styles like Legend location, color, font etc to the Chart exists in the presentation.
I am able to verify the Shape like
shape.HasChart == MsoTriState.msoTrue
But not able to get the Chart object out of the shape
Tried the following code, but received OLEFormat exception
Graph.Chart chart = (Graph.Chart)shape.OLEFormat.Object
Thanks in advance for your help.

Resolved the issue by selecting correct reference from the COM tab.
Answer from thread - http://social.msdn.microsoft.com/Forums/en-US/officegeneral/thread/06cea2b7-f169-4f8a-b53e-4319b12b5450

I know you are asking from (rather) .NET point of view but I'll show you some code from VBA PowerPoint. My goal is to show you object hierarchy. I believe it would be helpful enough for you to solve your problem.
Sub Chart_Elements()
Dim CHRT As Chart
'first shape in the 1st slide is chart
Set CHRT = ActivePresentation.Slides(1).Shapes(1).Chart
With CHRT
'to set legend position
.Legend.Position = xlLegendPositionBottom
'to set size of 1st entry in legend
.Legend.LegendEntries(1).Font.Size = 10
'and so on
End With
End Sub
If that answer doesn't help you at all please consider re-tagging your question.

Related

Cannot get correct type of chart: xlColumnStacked "variant 2" through vba code

I cannot assign the correct type of chart through VBA-code,
the type I need is "xlColumnStacked variant number 2"
Wanted variant
The code is run is the following:
ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.ChartArea.Select
ActiveChart.ChartType = 52 'or synonymously : ActiveChart.ChartType = xlColumnStacked
This do not occur if the plot is already of the stacked format (still type 52 / xlColumnStacked), either in "bar og column"-stacked mode.
However, if I change the series which are displayed in the plot, the chart goes to "default" and I struggle to find any vba-code wise way to get back to the format which I want (see picture).
The thing is that I print these, for 80 series, and I hope to make that automated...
Received vba variant
I have had the same problem with changing line style on markers in scatter plot mode too, which made the same changes to the lines (between the markers) in the scatter plott.
The problem is that the name of the object is overlapping with another name it seems.
I (and also a helper from Microsoft) found a partial solution:
https://answers.microsoft.com/en-us/msoffice/forum/all/cannot-get-correct-type-of-chart-through-vba-code/ef57be1a-fab5-42a9-8d18-4af79e0cd5b2?messageId=9f8d32f2-d53d-449f-aa44-6d076ed5a1fe

Chart is not visible in Parameter Variation Experiment page in AnyLogic

I created the parameter variation experiment to observe the changes in model outcome by changing specific parameter "ContactRateInfectious" values. Once I set the parameter values and Create default UI, no chart appeared. The image of my experiment page is given below.
I was expecting to see a chart that looked like this:
Let me know how can I achieve this.
Thank you.
You open the AnyLogic example models
You search for "Parameter Variation"
You open the first model you find (hint: it is called "Bass diffusion")
You copy the chart over
You figure out how they fill the chart from the model runs (see the Param-Var experiment properties in the example model)
You adapt it to your model
Done :)

Add Hyperlinks to Powerpoint from Matlab using ActiveX

Does anyone know how I can use matlab and activeX to add hyperlinks to powerpoint files?
There are two helpful posts on MatlabCentral, but they don't give me everything I need. The first explains how to create a powerpoint file using matlab: "Create Powerpoint Files with Matlab"
and the second shows how to use ActiveX to insert hyperlinks into Excel:"Add Hyperlink in Excel from Matlab" (See the second answer by Kaustubha)
I tried to merge the two answers. In powerpoint the slide objects have the .Hyperlinks attribute, but there is no .Add method for .Hyperlinks as there is in Excel.
Here is the code I have so far. I would like the link to appear in a table:
ppt = actxserver('PowerPoint.Application');
op = invoke(ppt.Presentations,'Add');
slide = invoke(op.Slides,'Add',1,1);
sH = op.PageSetup.SlideHeight; % slide height
sW = op.PageSetup.SlideWidth; % silde width
table = invoke(slide.Shapes, 'AddTable', 1, 3, 0.05*sW, sH*.2, 0.9*sW, sH*.60);
table.Table.Cell(1,1).Shape.TextFrame.TextRange.Text = 'www.stackoverflow.com';
% Add hyperlink to text in table using ActiveX
% slide.Hyperlinks - this exists but there is no add feature
invoke(op,'Save');
invoke(op,'Close');
invoke(ppt,'Quit');
delete(ppt);
Slide objects have a .Hyperlinks collection that you can examine to learn how many hyperlinks there are, where they point and so forth. To add a hyperlink you have to work with individual shapes or text ranges.
Sub AddAHyperlink()
Dim oSh As Shape
' As an example we're going to add hyperlinks to the
' currently selected shape.
' You could use any other method of getting a reference to
' a shape that you like, however:
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' Add a hyperlink to the shape itself:
With oSh
.ActionSettings(1).Hyperlink.Address = "http://www.pptfaq.com"
' you can also add a subaddress if required
End With
' Or add the hyperlink to the text within the shape:
With oSh.TextFrame.TextRange
.Text = "Hyperlink me, daddy, 8 to the click"
.ActionSettings(1).Hyperlink.Address = "http://www.pptools.com"
End With
End Sub
To access the text within a table cell you'd do as you're already doing:
table.Table.Cell(1,1).Shape.TextFrame.TextRange
or
table.Table.Cell(1,1).Shape
or
Set oSh = table.Table.Cell(1,1).Shape
then use the same code as I've shown above
Not sure if this is still an active request from anyone and potentially the program capability has changed; but for anyone else that might be interested it does appear to be possible to add links.
Here is an example from some code I wrote...The slide number/item references will need to get updated for your task but I think it covers the key points. In this example the goal was to add a hyperlink to another slide within the presentation.
hyperlink_text = sprintf('%0.0f, %0.0f, %s', Presentation.Slides.Range.Item(3+i).SlideID, Presentation.Slides.Range.Item(3+i).SlideIndex,Presentation.Slides.Range.Item(3+i).Shapes.Item(2).TextFrame.TextRange.Text);
The hyperlink text will look something like this, as a text string. '250, 4, Slide Title'
Presentation.Slides.Range.Item(3).Shapes.Item(2).Table.Cell(1+i,1).Shape.TextFrame.TextRange.ActionSettings.Item(1).Hyperlink.SubAddress = hyperlink_text;
For internal links the Hyperlink.Address field can be left blank.
It appears that the only thing that was missing from the prior answers was that when using Matlab to execute the powerpoint VBA you need to use ActionSettings.Item(1) to refer to the mouseclick action instead of ActionSettings(1) that was shown from basic powerpoint VBA.
Hopefully this can be helpful for anyone else still looking.
Note that I am currently using Matlab R2017A and Powerpoint in Microsoft 365 ProPlus

PNG area chart with Google Charts API?

I want to make an area chart with Google's Chart API, but it needs to be the PNG format one so I can build a PDF. Is that possible? The Chart Wizard does not seem to support area charts. Also, I want to do it in perl and the URI::GoogleChart module.
I spent a lot of time on this and discovered that I can do this:
See the example visually or the code below:
http://chart.apis.google.com/chart
?chxl=1:|week1|week2
&chxp=1,20,90
&chxr=0,0,120
&chxs=0,676767,11.5,0.5,l,676767|1,676767,9.5,0,_,676767
&chxt=y,x
&chs=300x278
&cht=lxy
&chco=FF9900,FF0000
&chds=6.667,100,0,110,0,100,0,118.333
&chd=t:-1|110,80|-1|43,32
&chdl=Total+Num|Special+Num
&chdlp=t
&chg=1,-1,1,0
&chls=2|2
&chma=8,0,0,7|54,2
&chtt=%23+Total+and+%23+Red+Items
&chts=000000,11.5
&chm=b,FF9900,0,1,0|B,FF0000,1,9,0
Where the last line b,color,startLine,endLine,0 means fill down to endLine and B,color,startLine,arbitraryNumber,0 means fill down to the bottom of the chart.
The solution came from Google's docs with some experimentation.

Crystal Reports - Accessing IGraphObject properties at runtime

I am using Visual Studio 2008 and Crystal Reports that is shipped with it. I would like to set certain properties of a chart such as Chart Title at Runtime. I have tried via ChartObject but there are only limited properties such as top, width, height etc. and not the properties I'm interested in. I included "craxddrt.dll" in my solution which provides IGraphObject but am obviously trying to cast the wrong class with it as it returns an invalid object reference...
(myReportDocument.ReportDefinition.ReportObjects[0] as IGraphObject).Title
Any help in retrieving the chart properties would be much appreciated...
thanks,
Paul
Instead of trying to set the chart title at runtime, set it to be blank and display a Crystal formula in the same place.