Excel VBA Duplicating a Slide in PowerPoint - excel

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

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.

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

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?

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.

Need to paste picture into Powerpoint then defining its location without selecting it

Here is a small snippet of my code where I paste a chart from Excel into Powerpoint as an enhanced metafile and then move it to a predefined location:
PPApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)
PPApp.ActiveWindow.Selection.ShapeRange.Top = ChartTop
PPApp.ActiveWindow.Selection.ShapeRange.Left = ChartLeft
The problem occurs when the user clicks the mouse between the first and second line of the code. Because the shape is no longer selected the vba code above fails.
Ordinarily this wouldn't be a problem but the macro as a whole runs for a long time with this happening many times in a loop. Is there a feasible solution to defining the pasted picture's location without the need to select the shape?
You nearly never need to select anything to work with it.
' if you're running the code from within PowerPoint:
Dim oSh as Shape
' or if from Excel:
Dim oSh as Object
' Get a reference to the newly pasted shape
' Don't miss the (1) at the end
Set oSh = PPApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)(1)
With oSh
.Top = ChartTop
.Left = ChartLeft
End With

VBA for Inserting a chart into a ppt slide

Below is a procedure that I use to pull charts into a ppt from excel spreadsheets. However one thing I cannot figure out is how to insert the picture into the "object" instead of just pasting it onto th screen. (ie if I did a ppLayoutFourObjects, and sent fours charts to this slide, before adding another, I need to know how to paste the chart into each designated rectangle shown from the 4 Objects selection). I know that the first one seems to always be rectangle five, I can't get the code right. Please help.
This is all 2003 Office.
sub xls2ppt()
'I use this to pull charts into ppt from excel
Dim xlApp As Object
Dim xlWrkBook As Object
Dim lCurrSlide As Long
Set xlApp = CreateObject("Excel.Application")
' Open the Excel workbook
Set xlWrkBook = xlApp.Workbooks.Open("X:\Users\Admin\Desktop\Budget Overview.xls")
' Copy picture of the 1st chart object onto the clipboard
xlWrkBook.Worksheets(2).ChartObjects(1).CopyPicture
' Get the slide number
lCurrSlide = ActiveWindow.Selection.SlideRange.SlideNumber
' Paste the picture onto the PowerPoint slide.
ActivePresentation.Slides(lCurrSlide).Shapes.Paste
' Close the open workbook without saving changes
xlWrkBook.Close (False)
xlApp.Quit
Set xlApp = Nothing
Set xlWrkBook = Nothing
End Sub
Thanks for any help. VBA for PowerPoint is my weakest, but I am really in need to pick it up for work! Thanks guys!
AFAIK you can't paste a chart "into an object" in PowerPoint, even through the UI. In Word, you can paste into a textbox or into a table cell, but not in PowerPoint.
What you need to do instead is position the 4 pasted charts so that they're the right size & position - and that's easy enough to do...
Set oSlide = ActivePresentation.Slides(lCurrSlide)
Set oShape = oSlide.Shapes.Paste
oShape.Top = 10
oShape.Left = 10
oShape.Width = 100
oShape.Height = 100

Resources