Change Link Excel sheet - graph object Powerpoint VBA - excel

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.

Related

How do I create dynamic hyperlinks in powerpoint?

I am trying to link a powerpoint presentation to data in excel. However the excel and powerpoint will change locations every day.
Here is my methodology so far:
- Copy from excel
- Paste special... paste link... As: Microsoft Excel Worksheet (code) object
This allows me to live edit the excel document and the powerpoint updates in real time.
However, when I change the location of the excel document, all 100+ links are broken.
How do I dynamically set the path of the links so they always follow the correct excel document, no matter where the excel document is? The excel document will never name change, neither will the powerpoint.
I have been looking for a solution for over 6 months...
Thank you.
I've got a page on my PPTFAQ site that includes code that might help:
Batch Search and Replace for Hyperlinks, OLE links, movie links and sound links
http://www.pptfaq.com/FAQ00773_Batch_Search_and_Replace_for_Hyperlinks-_OLE_links-_movie_links_and_sound_links.htm
Here's the specific code ... it deals with both hyperlinks and OLE links (ie, your Excel links). You can remove the hyperlink stuff if you don't need it and pass the new path as a parameter rather than getting it from an InputBox:
Sub HyperLinkSearchReplace()
Dim oSl As Slide
Dim oHl As Hyperlink
Dim sSearchFor As String
Dim sReplaceWith As String
Dim oSh As Shape
sSearchFor = InputBox("What text should I search for?", "Search for ...")
If sSearchFor = "" Then
Exit Sub
End If
sReplaceWith = InputBox("What text should I replace" & vbCrLf _
& sSearchFor & vbCrLf _
& "with?", "Replace with ...")
If sReplaceWith = "" Then
Exit Sub
End If
On Error Resume Next
For Each oSl In ActivePresentation.Slides
For Each oHl In oSl.Hyperlinks
oHl.Address = Replace(oHl.Address, sSearchFor, sReplaceWith)
oHl.SubAddress = Replace(oHl.SubAddress, sSearchFor, sReplaceWith)
Next ' hyperlink
' and thanks to several astute user suggestions, let's fix OLE links
' and movie/sound linkes too
For Each oSh In oSl.Shapes
If oSh.Type = msoLinkedOLEObject _
Or oSh.Type = msoMedia Then
oSh.LinkFormat.SourceFullName = _
Replace(oSh.LinkFormat.SourceFullName, _
sSearchFor, sReplaceWith)
End If
Next
Next ' slide
End Sub
You can link data from a saved Excel spreadsheet or copy cells from an Excel spreadsheet into your Microsoft PowerPoint 2010 presentation.
https://support.office.com/en-us/article/import-data-from-excel-into-powerpoint-3ec295e9-1bfd-47ff-8d7d-8b838caef853
Use the "Name manager" function in "Formula" and define the name of the table/certain cells you want to paste on PowerPoint.
So that when you paste "Excel Object" on PPT, the link will not simply be the location of the table in a worksheet (something like R1C1), but the name you defined and will not be affected if you move the table around.

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

Copy-Paste image to chart improperly pastes a blank image

I have a tangential question to this one: Using VBA Code how to export excel worksheets as image in Excel 2003?
Specifically, when the macro pastes the copy of the range to the chart, the image is blank, even though the copied range contains 5 charts and some formatted cells. When I perform the exact same steps manually it all works as expected.
Even weirder, I've recorded the whole process except the export step. When I run the recorded macro, it works. But when I copy the code from the recorded macro inside the For Each loop below, and tweak it to point to the sheet being worked by the macro (i.e. replacing "ActiveSheet" with "t") the macro doesn't work again.
I even went so far as to just invoke the recorded macro after using the For Each to move to each sheet, still getting a blank image pasted.
I'd appreciate any help on this.
My code:
Sub ExportCharts()
Dim Rng As Range
Dim S As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Dim EName As String
Dim CO As ChartObject
Dim C As Chart
Dim temp As String
Application.ScreenUpdating = False
'Iterate through the sheets in the workbook
For Each t In wb.Worksheets
'Capture the sheet
Set S = t
S.Activate
'Set the range to be exported
Set Rng = S.Range("A1:Z60")
'Copy range as picture onto Clipboard
Rng.Select
Rng.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
'Build the chart/file name
EName = S.Name & " Quality Charts"
'Create an empty chart with exact size of range to be copied
S.Range("$AA$1:$AC$2").Select
ActiveSheet.Shapes.AddChart.Select
Set C = ActiveChart
temp = Right(C.Name, Len(C.Name) - 1 - Len(S.Name))
S.Shapes(temp).Height = Rng.Height
S.Shapes(temp).Width = Rng.Width
'Paste into chart area, export to file, delete chart
'C.Activate
With C
.Paste
.Export "\\COMPUTERNAME\Users\USERNAME\Desktop\My Documents\" & EName & ".jpg"
'Note the above is an actual hard coded path in my code (yes I want it hard coded)
End With
C.Delete
Next
Application.ScreenUpdating = True
End Sub
So I finally figured out the issue. The line turning off screen updating is the issue.
One would assume that's because I say copy as it appears on the screen (why MS didn't just allow for that command to use the display as it appeared at the last screen update is beyond me, but it's not exactly like Excel is bug free).
In any event, commenting out that line results in a good paste.
As a note for those who have more going on in there macros and want/need screen updates off in order to get a reasonable run speed, after I figured out the problem I tried reactivating screen updates before the copy as picture, then turning it off again immediately afterwards, and that worked.

Collating Excel Worksheets Imbedded in Powerpoint

Background: I am on the backend of an effort to capture and collate data collected in PowerPoint template form. A template was distributed. The result is ~150 PowerPoint 2010 presentations of ~30 slides each. ~15 of slides in each presentation contain an imbedded XLS.
Benefit to community: Examples of PowerPoint to Excel techniques and in general MS Office techniques vs. solely one MS Office tool.
Problem: I'm only an introductory VBA developer. I seem to find many examples how to get Excel data to PowerPoint, but not much (!) about this seemingly backward approach of data from PowerPoint into Excel. PeltierTech.com gets me close. I found some texts but need a solution before I can get through them.
Need:
1) Loop through all presentations (.PPTX) in a folder (open/close)
2) Inspect each slide in each presentation for an imbedded XLS
3) If found
a) Copy the imbedded source XLS range (not image)
b) Find the last row of the target XLS tab
c) Write the .PPTX name into tab column A
d) Paste the source XLS into target Excel column B
Finally I would prefer the "host" VBA be Excel.
The ideal result is a single .XLSX with ~15 tabs. The resultant data can be scrubbed for unique headers and converted into a pivotable dataset.
This doesn't appear the most to be the most challenging exercise. I think I'm hung on combining the two object models in a single set of procedures. (Yes, the references are correctly set ;-) )
THANK YOU!!!
Here is an example I wrote to extract one chart data from PP file (code is in a excel module). I've commented the code so you will be able to build your own loops to this, but the mechanics of extracting chart data from PP-file via Excel-VBA is as in the following block -
'"Microsoft PowerPoint xx.x Object Library" needed to be installed
'
Sub OpenPPandCopyChartData()
'Create an instance of Powerpoint
Dim PPT As Object
Set PPT = CreateObject(Class:="PowerPoint.Application")
'Open the powerpoint file
Dim pp As PowerPoint.Presentation
Set pp = PPT.Presentations.Open(Filename:=ThisWorkbook.Path & "\Presentation1.pptx", ReadOnly:=msoTrue)
'Select the wanted slide
Dim ps As PowerPoint.slide
Set ps = pp.Slides(1)
'Select the shape
Dim sh As PowerPoint.Shape
Set sh = ps.Shapes(2)
'You can make your own loop to check all the shapes if they contain a chart by using HasChart Method
Debug.Print sh.HasChart = msoTrue
'Select the shape that has chart and activate
sh.Chart.ChartData.Activate
'Set the activated workbook to a variable
Dim wb As Workbook
Set wb = sh.Chart.ChartData.Workbook
'Select the sheet the data is contained
Dim ws As Worksheet
Set ws = wb.Sheets(1)
'Select the range and copy
ws.UsedRange.Copy
'Set where to copy
ThisWorkbook.Sheets(1).Range("A1").PasteSpecial xlPasteValues
wb.Close
pp.Close
PPT.Quit
End Sub
The code opens the PPT.file in the same folder that the Excel file is in and selects Slide(1) and Shape(2) from that slide. Then it activates the chart data (if not present this code will cause a subscript out of range error, so on your own code a data checking will be needed) and copies it to the Excel that the sub was started in.

When placing multiple charts in a chart sheet, how do I change chart properties using vba?

I am able to place multiple charts on a chart sheet by creating an empty chart sheet and then setting the Location of the charts to that empty chart sheet.
'This creates multiple charts within a single chart sheet!
Charts.Add 'Creates empty chart page when empty cell is selected
'Keep track of chart page for later reference
Dim chartSheet As String
chartSheet = ActiveChart.Name
.... 'Create three separate charts with data here
'Now place these charts within our empty chart page
Set chart1 = chart1.Location(Where:=xlLocationAsObject, Name:=chartSheet)
Set chart2 = chart2.Location(Where:=xlLocationAsObject, Name:=chartSheet)
Set chart3 = chart3.Location(Where:=xlLocationAsObject, Name:=chartSheet)
All of the code works up until this point. The chart sheet contains all 3 charts, although the charts are all overlapping. When I try to adjust the position of the charts...
'This code fails to run!
chart1.Parent.Top = 0
chart1.Parent.Left = 0
The code returns a runtime error of Object doesn't support this property or method. I know it's possible to move the graphs manually by clicking and dragging, and I know that the above code works if the chart is within a normal worksheet. But for some reason, this code fails when the charts are within a chartsheet. Is there any way to get VBA to do what I want?
Thanks for the help.
After a bit of Experimentation I belive your code isn't running because chart1 is it's own worksheet.
When I drilled into a selected chart i've noticed the following difference. If the chart is embedded (Object in another sheet) the Parent is a Object/ChartObject which has properties Top andLeft (it's relative location in the worksheet). If the chart is it's own worksheet, the Parent is actually an Object/Thisworkbook' which does not haveToporLeft' properties. This makes sense because when you create a chartSheet the chart fills the entire sheet area and thus cannot have a relative postion.
If you insert a breakpoint on chart1.Parent.Top = 0 and view locals.... you can see what I have explained above.
EDIT: to suggest something reagrding your ask in the comments.
The Children are contained in the 'Shapes' Collection of the worksheet object. i used the following to show the names of all shapes in a worksheet
For i = 1 To ActiveWorkbook.ActiveSheet.Shapes.Count
temp = temp & i & " " & ActiveWorkbook.ActiveSheet.Shapes(i).Name & vbCr
Next
MsgBox temp
From there you should be able to move and format etc.

Resources