How to change a think cell object's name through VBA? - excel

I am putting together a VBA module that calculates some data in Excel and pastes it in a powerpoint presentation.
In this powerpoint presentation, I have some think cell charts, that I know I can update with the help of the tcaddin (see code below). What I didn't find though, is how I can identify a think cell chart on a given slide, or change the name of this object. The reason I need to do that is that I want to copy a chart and update it with another set of data than the one already used, but if I do that with the updatechart method, it will update both the original chart and the new one. I don't know before hands how many charts I will have total, so I can't produce them and name them in advance.
I've tried to use "ActiveWindow.Selection.ShapeRange.Name" to identify the name of my think cell object, but this doesn't work (Error '-21447188160 (8048240) : selection (unknown member) : invalid request. Nothing appropriate is currently selected)
I've searched for similar questions, but the ones I found kinda matching my problem were not answered. Think Cell's help on the subject is not so deep (https://www.think-cell.com/en/support/manual/exceldataautomation.shtml) and I couldn't find any solutions in there.
Dim tcaddin As Object
pptFileName = WSExec.Range("B18").Value
' Get the think-cell add-in object
Set tcaddin = Application.COMAddIns("thinkcell.addin").Object
'Get powerpoint object:
On Error Resume Next
Set ppApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Get the presentation :
If ppApp Is Nothing Then
Set ppApp = New PowerPoint.Application
Set ppPres = ppApp.Presentations.Open( _
Filename:=pptFileName, _
Untitled:=msoFalse, _
WithWindow:=msoTrue)
Else
Set ppPres = ppApp.Presentations.Item(1)
End If
Call tcaddin.UpdateChart(ppPres, "ChartTrafficXEvol", WSExecSum.Range("J33:N52"), False)
I have no output to provide, but what I would like is some property on the tcaddin object, that would allow me to change the name of a chart or create a new one (and setting its name).
Thanks for your help.

The first parameter in UpdateChart() can either be an entire presentation or a slide range. This means that you can create one template slide with a manually named think-cell chart and then copy the entire slide via VBA. The think-cell chart on the newly copied slide will have the same name as the original. You can then run UpdateChart() specifying only the new slide as the first parameter to update its data.

VBA is useable only on native shapes, not Think-Cell shapes. Here is Think-Cell's statement on the subject: Can I use my VBA macros with think-cell elements?

Related

Copy/Paste Special as Microsoft Graphic Object from Excel to Powerpoint

I have a report I create each week that copies a series of charts into a series of slides. Currently, I have VBA code that copies and pastes the charts as images, but the client has asked that the charts be graphic objects so they can inspect the source data from the PPT. Below is a simplified version of the relevant code:
Sub CreatePPT
Dim PP As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Set PP = New PowerPoint.Application
Set PPPres = SCPP.Presentations.Open("C:\filepath\Template.pptx")
PP.Visible = True
'Select the slide I want to paste the chart to (I am not really sure why I need this line, but get an error if I do not have it)
PPPres.Slides(1).Select
'Copy the range where the chart is located
Sheets("Charts").Range("c10:D20").CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Paste the chart to the slide
PPPres.Slides(1).Shapes.Paste.Select
Set PPPres = Nothing
Set PP = Nothing
End Sub
I have tried using paste special, but none of the available data types is anything like the "Paste as Graphic Object" that is available when I manually copy/paste special from excel to ppt manually. Does anyone know it is possible to replicate this type of paste special using VBA? Thanks in advance!
Instead of copying a range (presumably under a chart) as a picture, copy the chart itself, regular copy, not copy picture. Then do a straightforward paste:
Sheets("Charts").ChartObjects("Chart 1").Chart.ChartArea.Copy
PPPres.Slides(1).Shapes.Paste
But if you don't send along the Excel source workbook, and if they don't set up the links properly, they won't be able to view the data in Excel.

Change Link Excel sheet - graph object Powerpoint VBA

I am facing troubles with VBA coding.
I have an excel file with various sheets with data and graphs. These graphs are linked to a Powerpoint (graphs have been copied and paste "with link" as objects).
The issue, is that I now have a huge Powerpoint of more than 130 slides with about 18 graphs on each slide... So more than 2000 graphs.
I would like to change the name of my sheets and also to duplicate some slides to populate the graphs with filtered data.
My issue:
- If changing the sheet name, of course the link is broken. Updating everything by hand with the UI is just impossible;
- When duplicating a slide in PowerPoint, the graphs are still linked to the same Excel sheet as the original slide - the only way to change the link is to delete all graphs, duplicate the sheet in Excel - populating with new data - copying-pasting with link again each graph one by one into PowerPoint.
I have tried to use a macro but... it changes the whole address of the link, deleting all sheets information. Is there a way to modifiy the hard address but keeping the same excel file - only changing the sheet?
Here is what I am trying to use to replace the sheet "T3" by the sheet "100s". The macro runs without error but then all the objects are replaced by a copy of the WHOLE "100s" worksheet from my excel file :(
Sub EditPowerPointLinks()
Dim oldFilePath As String
Dim newFilePath As String
Dim pptPresentation As Presentation
Dim pptSlide As Slide
Dim pptShape As Shape
'The old file path as a string (the text to be replaced)
oldFilePath = "\\Server\01xxxx\xxx\xx\X 4.xlsx!T3"
'The new file path as a string (the text to replace with)
newFilePath = "\\Server\01xxxx\xxx\xx\X 4.xlsx!100s"
'Set the variable to the PowerPoint Presentation
Set pptPresentation = ActivePresentation
'Loop through each slide in the presentation
For Each pptSlide In pptPresentation.Slides
'Loop through each shape in each slide
For Each pptShape In pptSlide.Shapes
'Find out if the shape is a linked object or a linked picture
If pptShape.Type = msoLinkedPicture Or pptShape.Type _
= msoLinkedOLEObject Then
'Use Replace to change the oldFilePath to the newFilePath
pptShape.LinkFormat.SourceFullName = Replace(LCase _
(pptShape.LinkFormat.SourceFullName), LCase(oldFilePath), newFilePath)
End If
Next
Next
'Update the links
pptPresentation.UpdateLinks
End Sub
Would anyone have an idea on how to change only the sheet name and keeping all the object names after?
Thanks a lot,
Arthur
In fact, the formula works fine. The replacement of link didn't work because in the original sheet that I copied, the first object in the selection pane was Graph 3. When copying the sheet, Excel automatically tries to make it start at 1 so Graph 3 became Graph 1. Then, when replacing the links, the graphs didn't match.
To make this formula work, make sure in the Selection Pane in Excel that your graphs are named the same way between the original sheet and the new one.

Excel VBA Duplicating a Slide in PowerPoint

I am working on exporting Contents from Excel to PowerPoint. I have a blank formatted slide in my PowerPoint presentation which I Need to duplicate everytime and write on it. The Problem is that my code adds a new slide ahead of the current slide which is posing Problems in writing the Contents to the exact slide number. I want that the new slide should be added after the current slide.
Set pptSlide = oPPTApp.ActivePresentation.Slides(1).Duplicate.Item(1)
oPPTFile.Slides(SlideNum).Select
Set oPPTShape = oPPTFile.Slides(SlideNum).Shapes("Table 1")
any Suggestions ?
There's almost NEVER a good reason to select anything when automating PPT.
Assuming a shape named Table 1 on Slide 1, this should do what you want:
Dim oSl As Slide
Dim oSh As Shape
Set oSl = ActivePresentation.Slides(1).Duplicate()(1)
Set oSh = oSl.Shapes("Table 1")
ActivePresentation.Slides.Range(1).Cut
ActivePresentation.Slides.Paste -1
this function will move the first slide to the last. so it stands to reason you could figure out a formula to keep track of where you want the slide inserted.
if you need to know how many slides are in the range it's
ActivePresentation.Slides.Range.count

Modify Excel VBA code to paste into specific ppt slides

I am new to the forum, and a few months new to VBA. All my coding experience is in Matlab, which so far I find much easier than VBA. I have read the forum rules, and searched for this answer high and low, but to no avail.
I am trying to paste different excel ranges into different slides of a PowerPoint. I want to modify the code below (taken in snipets from different forum answers) so that the range is pasted into the corresponding PowerPoint slide I denote, regardless of what the active slide is. I have tried "gotoslide" and that didn't work, and used ppviewnormal, but that also didn't work. This code will then be repeated for different ranges on different slides. So far the code will only work when I am on the slide it was written for, and I know it has to do with issues regarding the active slide, but cannot seem to figure out how to modify the code. Thank you in advance for your help!
' Desired Range in Excel to be copied into ppt
Sheets("Summary").Activate
Range("A5:H40").Select
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
' Denote slide where range is to be copied
Set PPSlide = PPPres.Slides(5)
' Copy the range as a picture
Selection.CopyPicture Appearance:=xlScreen, _
Format:=xlPicture
' Paste the range
PPSlide.Shapes.Paste.Select
Remove ".Select" from last line
This is shorter version of your code:
' Initialize PowerPoint Object Library
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
' Copy the range as a picture
Sheets("Summary").Range("A5:H40").CopyPicture Appearance:=xlScreen, Format:=xlPicture
' Paste the range
PPPres.Slides(1).Shapes.Paste

Import excel chart with spin button to powerpoint

I have a chart in excel with a spin button that changes a value to change the chart when pressed. I can import them into powerpoint by using insert -> object but this doesn't make the spin button usable.
I'm trying to get it so when showing the presentation I can click the spin button and the chart will correspondingly change, just like it does in excel. Is this possible?
THe spin button is connected to a macro and that's what's causing the chart to change in Excel.
You can copy the code from Excel and place it in a standard module in the Powerpoint file.
You will likely have to make some tweaks -- more or less, depending on how well the original macro was written (e.g., if it is full of ActiveSheet.This and ActiveChart.That then it's going to need a bit more tweaking than if it uses a Chart or ChartObject variable to represent the chart.
Here is an example of interacting with a powerpoint chart:
Sub TEST()
Dim sld As slide
Dim cht As Chart
Dim srs As Series
Set sld = ActivePresentation.Slides(1) 'Modify to be the correct slide #'
'You can hardcode the shape by index if you know it, otherwise'
' this method will apply to the FIRST chart object found on the slide'
For i = 1 To sld.Shapes.count
If sld.Shapes(i).Type = msoChart Then
Set cht = sld.Shapes(i).Chart
Exit For
End If
Next
'use a variable to interact with the Series:'
Set srs = cht.SeriesCollection(1) '<Modify as needed.'
'specify a particular formula for the series:'
'your macro likely changes the chart's source data/formula information'
' so something like this:'
srs.Formula = "=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$5,Sheet1!$B$2:$B$5,1)"
'Or you can display the series formula:'
MsgBox srs.Formula
'Or you can toggle the series axis group:'
With srs
If Not .AxisGroup = xlSecondary Then
.AxisGroup = xlSecondary
Else
.AxisGroup = xlPrimary
End If
'etc.'
'Basically anything you can do to an Excel chart in VBA, you can do in PPT.'
End With
End Sub
When you copy/paste anything from Excel to PowerPoint, you get an OLE object. What appears in PowerPoint is a Windows metafile picture of whatever you copied from Excel. You'd have to either activate the Excel content (doubleclick it within PPT's normal view or give it the appropriate Action Setting to have it activate during a slideshow). Then the spinner should also work within the instance of Excel that launches.
Or you could create another spinner in PPT and have it run code to modify the chart (using David's example code as a basis).

Resources