How add Excel.ChartObject that is not a chart sheet with AddOleObject? - excel

This is not a duplicate question because the other questions do not have solutions for regular chart objects but only cover chart sheets.
Question: I want to add an Excel chart (i.e. NOT a chart sheet) to PowerPoint by using AddOleObject. I do not want to use a clipboard solution for this.
I tried the following:
oSlide.Shapes.AddOLEObject(FileName:=$"{oChartObject.Parent.Parent.Path}\{oChartObject.Parent.Parent.Name}!{oChartObject.Parent.Name}!{oChartObject.Name}", Link:=Microsoft.Office.Core.MsoTriState.msoFalse)
the FileName:= results in the following string:
C:\Users\Me\Desktop\SomeWorkbook.xlsx!Sheet2!NameOfMyChartObject
But this fails (com exception E fail) and I do not understand how to fix it.

Related

VBA: Automating Loading of Data from Excel into PowerPoint Internal Chart Data

I am looking to script the automated loading of data from an Excel Worksheet directly into the embedded Chart Data Source within PowerPoint - as opposed to copying the chart in Excel and pasting the linked chart into PowerPoint.
I have attached a link to an image which demonstrates (I hope) my intention.
I tried to implement some of the code on :
www.think-cell.com/en/support/manual/exceldataautomation.shtml#x28-21500022.1
But not entirely sure if this is the right approach, plus I am having issues debugging the code for:
Dim tcaddin As Object
Set tcaddin = _
Application.COMAddIns("thinkcell.addin").Object
And genuinely confused as to where in my code to implement what it calls the signature:
tcaddin.UpdateChart( _
pres As PowerPoint.Presentation, _
strName As String, _
rgData As Excel.Range, _
bTransposed As Boolean _
)
Genuinely not sure if I've gone about this the wrong way or if it's even possible, but some new and exciting approaches would be massively appreciated!!
If you want to add parts of an excel document to a powerpoint, you can paste the data so that it embeds and links to that document.
https://support.office.com/en-ie/article/insert-excel-data-in-powerpoint-0690708a-5ce6-41b4-923f-11d57554138d
Look at the above link and try the section Link a section of data in Excel to PowerPoint.
Then everytime you open the powerpoint it will ask if you want to update the links that are attached to the excel document.

How to control excel chart source range through VBA

I want to be able to control and update the source (range) of an excel chart through VBA. The chart is ultimately going to be presented in a powerpoint presentation and the whole process shall update automatically.
I have tried using tables which doesn't give me the level of control I want. For example if i delete one datapoint the chart doesn't "shrink" accordingly.
ActiveChart.SetSourceData (Sheets("Sheet2").Range("E5:E9"))
The current script doesn't run. But I just want to "set" a source range for a particular chart, preferably accessed by name. And if new datapoints are added I just run the script again and the new range will be the top cell in the column down to .End(xlDown)
Thanks guys! I manged to find a solution that works for me and made a function of it, if anyone else has the same problem. Nothing fancy but you can set a range like "A2:A14" and the chart will update with the new range. The function is called by sheet name, chart name and range like below:
Call changecharts("Sheet2", "Chart1", "A2:A11")
Public Function changecharts(sheeet As String,chartname As String, rnge As String)
Worksheets(sheeet).ChartObjects(chartname).Activate
With ActivateChart
ActiveChart.SetSourceData Source:=ActiveWorkbook.Sheets(sheeet).Range(rnge)
End With
End Function

VBA Paste Special on Existing Chart

I would like to use VBA to paste a range of data into an existing chart, using the options shown in the following screenshot:
If I record a macro whilst doing so manually, the code only states ActiveChart.Paste. Thus, when I re-run this code the series is pasted regularly without the 'Series Name In First Row' deactivated. How can I code this correctly? I haven't found much in the way of help in my research so far.
Well, I tried a simple code and it works.
Suppose you have a data like below and you made a simple Clustered Column Chart:
Now you want to paste special Data2 as in your screen shot.
VBA code which works at my end is below:
Sub test()
Dim ch As Chart: Set ch = Sheet1.ChartObjects(1).Chart
Range("A1:A4,C1:C4").Copy '~~> you need to include the x axis labels when copying
ch.SeriesCollection.Paste RowCol:=xlColumns, SeriesLabels:=False, _
CategoryLabels:=True, Replace:=False, NewSeries:=True
End Sub
Result:
I don't know what chart you're working on or how you want it constructed.
Above just shows how to execute Copy and Paste Special of data from a Range to an Existing Chart. HTH

Excel VBA - pasted object into Powerpoint loses selection for manipulation

Just to let you all know, I'm a beginner to VBA and I'm using Excel and PowerPoint 2013.
The aim: I'm writing code in Excel VBA to paste a chart from Excel into PowerPoint. Then when it's pasted, move it to a set place within the slide.
The code I used: The framework of the code I started with is in http://peltiertech.com/Excel/XL_PPT.html#chartppt, the part called 'Paste the Active Excel Chart into the Active PowerPoint Slide (Early Binding)'.
The main difference is around the 'Paste' part, this is instead a 'PasteSpecial' and my code is as follows:
.
Charty.CopyPicture
ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)
' Setting the correct position on the slide
ppApp.ActiveWindow.Selection.ShapeRange.Top = ChartTop
ppApp.ActiveWindow.Selection.ShapeRange.Left = ChartLeft
(Note. ChartTop and ChartLeft have been defined earlier)
The issues: I'm aware that after the PasteSpecial it doesn't have a select statement. However, (1) When I run the above code I get a runtime error '-2147188160' saying the Selection.ShapeRange is an invalid request (in the 'ChartTop' line), ie. it isn't selected so it can't be moved. (2) When I add a select statement to the 'Paste.Special' line I get an 'Object required' error on the new 'PasteSpecial' line, ie. it doesn't recognise it as a paste select statement.
The odd part: When I run the same macro on other people's computers using the exact same Excel and PowerPoint files it works (without the select statement in the 'PasteSpecial' line). I've looked at 'Tools' => 'References', and both of our computer have the same settings.
The questions: Is there a setting that I have overlooked on my computer which prevents the code from working? Is there a way after I've pasted it to reselect it before it is moved (I should tell you that the macro is to paste in many graphs into ppt simultaneously)? How come for other people at my work it works on their computers without the 'select' part of the PasteSpecial statement? How come when I use the Shapes.PasteSpecial (ppPasteEnhancedMetafile).Select statement, it doesn't recognise it as an object?
I hope that's clear.
Thanks in advance.
Dim oSh as Shape
Set oSh = ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)(1)
Now you don't need to select anything (which is best avoided whenever possible). Just manipulate the pasted shape, oSh, directly.
The curious syntax (the (1) at the end) is because Shapes.Paste or .PasteSpecial returns a shaperange, not a shape; the (1) causes it to assign the first shape in the range to the oSh variable. Since there's only one shape in the range, that suits us just fine.

How to insert an excel chart into Word using AddOLEObject

I'm trying to create a linked OLE Object in a Word document using VB.Net.
Existing code uses InlineShapes.AddOLEObject(FileName:="abc.xlsx", LinkToFile:=True, Range:=Some Word Range) to insert a worksheet into a Word document.
I need more control than this. To select a range of cells I've found that extra information after the filename can be useful, for example: FileName:="abc.xlsx!sheet1!R1C1:R20C5"
Is there a way to specify a specific chart within a worksheet? So can I specify the second chart on the worksheet as the object to link to?
Thanks.
Thanks for your help Mark.
I eventually figured out that if the Chart is in it's own sheet, rather than an object in Sheet1, then the AddOLEObject code works correctly with the following setting:
FileName:="abc.xlsx!Chart1"
I'm happy with this solution.
A chart will either be a whole worksheet so address as per your sheet1 e.g. abc.xlsx!sheet1 or an object on a sheet so use the object name e.g. abc.xlsx!sheet1!chart_object

Resources