How to "Refresh Data" via VBA in Power Point? - excel

so far I have tried the Chart.Refresh and Chart.Update and also ChartData.UpdateLinks and neither work.
My question is similar to this one only that this code did not work for my ppt
How to update excel embedded charts in powerpoint?
If i could Record Macro like in Excel the steps would be:
Select Chart
Chart Tools > Refresh Data
This is code is what I have managed to write but it fails at "gChart.Application.RefreshData":
Sub refreshchart()
Dim ppApp As PowerPoint.Application, sld As Slide
Dim s As PowerPoint.Shape
Dim gChart As Chart, i As Integer
ppApp.Visible = True
i = 3
Set sld = ActivePresentation.Slides(i)
sld.Select
For Each s In ActivePresentation.Slides(i)
If s.Type = msoEmbeddedOLEObject Then
Set gChart = s.OLEFormat.Object
With gChart.Application
gChart.Application.Refresh
Set gChart = Nothing
End If
Next s
End Sub
The Integer i is included to go from i=1 To 73, but as a test i am using Slide 3. Not all Slides have Charts but most of them have 4 Charts (65 out of 73).

I changed the code a little bit and with this little change, the refresh of the charts works again automatically.
Many times, if you share your excel ppt combo the links break and after restoring them the automated chart refresh doesn`t work.
With the downstanding macro the automated refresh will work again:
Sub REFRESH()
Dim pptChart As Chart
Dim pptChartData As ChartData
Dim pptWorkbook As Object
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set pptChart = shp.Chart
Set pptChartData = pptChart.ChartData
pptChartData.Activate
shp.Chart.REFRESH
On Error Resume Next
On Error GoTo 0
End If
Next
Next
Set pptWorkbook = Nothing
Set pptChartData = Nothing
Set pptChart = Nothing
End Sub
The code below is in a macro in the Excel workbook which also contains the source data. Not sure if the code would be the same running it from PowerPoint. I simply open my Excel Workbook and then have it update the PowerPoint for me.
I have been looking forever to find an answer to this and finally managed to get it to work with a ton of reading and trial-and-error. My problem was that I have a PowerPoint with a lot of graphs that were created with CTRL+C and CTRL+V, so none of them are linked.
This is how I got it to work:
Dim myPresentation As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim myChart As PowerPoint.Chart
For Each sld In myPresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set myChart = shp.Chart
myChart.ChartData.Activate
myChart.Refresh
End If
Next
Next
I don't know if there is unnecessary code in there but I am just happy that I finally got it to work so I'm not touching it anymore.

This code worked. But it works only if both files are open (the excel if its only one): The Power Point and the Excel with the data. It actually Refreshes all charts one by one.
Sub updatelinks()
Dim sld As Slide, shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
On Error Resume Next
shp.LinkFormat.Update
Next
Next
MsgBox ("Graficos actualizados con éxito")
End Sub
So, If the Excel is on a shared location, the code wont work because it takes too much time to retrieve the data. Im still looking for a way to do this. Thanks!

This may help, It opens and closes the embedded Excel object
For Each s In ActivePresentation.Slides(i)
If s.Type = msoEmbeddedOLEObject Then
s.Select 'select the object
s.OLEFormat.Activate 'Activate it (like 2x click))
ActiveWindow.Selection.Unselect 'To let it close
ActiveWindow.View.GotoSlide s.Slideindex 'make current slide active
End If
Next s

Related

PowerPoint Active.Presentation.Updatelinks not updating

I have a presentation with chart links to an Excel file, I have this simple code in an Excel file that opens the linked file, opens the PP, updates and removes the links, saves and closes. The issue with this code is that the links are not always updating. When I run this from the VB editor it works fine but I have this run on a daily schedule (using Workbook_Open and Application.OnTime) and it will not update the links when automatically running each day. All other aspects of the code run without issue and there are no errors. Here is the code, appreciate any assistance.
Private Sub Update_PP()
Dim PPT As New PowerPoint.Application
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Workbooks.Open FileName:="C:\Data Files\Excel File.xlsb"
PPT.Visible = msoTrue
PPT.Presentations.Open "C:\Data Files\Template.pptx", ReadOnly:=msoFalse, WithWindow:=msoTrue
PPT.ActivePresentation.UpdateLinks
For Each sld In PPT.ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.Type = msoLinkedOLEObject Then
shp.LinkFormat.BreakLink
End If
Next shp
Next sld
PPT.ActivePresentation.SaveAs FileName:="C:\Data Files\New Template.pptx"
PPT.ActivePresentation.Close
PPT.Quit
Set PPT = Nothing
End Sub

Updating Powerpoint Charts linked to Password Protected Excel File

I ve got a password protected excel workbook and a powerpoint presentation which has many charts linked to this workbook. What I am trying to do is updating all charts in the presentation without entring a password. I tried to reach data via ChartData object but couldn't find out how to access workbook with a password written in the code. I had also tried creating an excel object however this time after setting workbook as unprotected I couldn't define the code that refresh links between charts and data.
I appreciate any effort that helps me to solve this problem.
I had found this code from this link and I inserted myChart.ChartData.Workbook.UnProtect Password:="1234", writeResPassword:="1234", ReadOnly:=False, Updatelinks:=3. Unfortunately it is still asking for password.
Sub update2()
Dim myPresentation As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim myChart As PowerPoint.Chart
Dim Wb As Object
Dim App As Object
Set myPresentation = ActivePresentation
For Each sld In myPresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set myChart = shp.Chart
myChart.ChartData.Activate
myChart.Refresh
Set Wb = myChart.ChartData.Workbook
Set App = Wb.Application
Wb.Close (0)
End If
Next
Next
App.Quit
End Sub
I think as it gets to the line myChart.ChartData.Activate then password input box is triggered and from that point the rest of the code doesnt work.
I assume that when we reach chart data through powerpoint it runs an explicit app. If so how I can pass the password dialog in that app.

Pasting Excel data in PowerPoint chartData

I need to create a PowerPoint slideshow with graphs. Data source for each graph is an Excel file.
I searched for a similar answer, but none seemed exactly my case.
The presentation is quite long and heavy.
A copy of the presentation (without macros) will be saved as a different file. It needs to be modifiable, so I copied data from Excel to dataChart for each chart in the slideshow.
First and main question: As I try to copy data, I get one of two errors. "Object doesn't support this property or method (Error 438)" or, in some other combinations, "Subscript Out of Range (Error 9)".
Second and minor question: any idea of a better structure for the whole operation?
Private Sub CommandButton1_Click()
Dim sld As Slide
Dim shp As Shape
Call openxcl 'see below
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart = msoTrue Then
With shp
'doing other things
End With
With shp.Chart
.ChartData.Activate
.ChartData.Workbook.Worksheets(1).Cells.Clear
.ChartData.Workbook.Worksheets(1).Range("A1:E6").Paste 'Here I get my error
.ChartTitle.Text = shp.Chart.ChartData.Workbook.Sheets(1).Range("A1").Value
.ChartData.Workbook.Close
End With
End If
Next shp
Next sld
End Sub
Sub openxcl()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\path\Source.xlsx", True, False
AppActivate ("Source.xlsx - Excel")
Dim test As Workbook
Set test = ActiveWorkbook
test.Sheets(1).Activate
With test.Sheets(2).Range("A1:E6")
'.Select
.Copy
End With
Set xlApp = Nothing
Set test = Nothing
End Sub

Refresh Embedded Excel Data in Powerpoint Charts

I'm trying to automate the creation of a PowerPoint deck that I create a few times a week. I use a program called Alteryx to update the embedded excel files for all my charts in PowerPoint and that part works great. The problem that I have is that once I open PowerPoint, all the charts look the same as they originally were. It's only when I click edit data, that PowerPoint seems to read the excel changes and updates the chart. I have over 50 charts that I need to update, and clicking edit data on each one is very time consuming.
Is there a macro that can be created that will refresh all charts in my deck?
Thank you in advance for your time!
Update 1
I have tried using the following code from another post, but it results in 50 open workbooks.
Dim pptChart As Chart
Dim pptChartData As ChartData
Dim pptWorkbook As Object
Dim sld As Slide
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasChart Then
Set pptChart = shp.Chart
Set pptChartData = pptChart.ChartData
pptChartData.Activate
shp.Chart.REFRESH
On Error Resume Next
On Error GoTo 0
End If
Next
Next
Set pptWorkbook = Nothing
Set pptChartData = Nothing
Set pptChart = Nothing
I tried using the following code to open and close all the embedded workbooks, but it results in an error at this part "ActiveWindow.View.GotoSlide s.Slideindex"
Sub refreshchart()
Dim ppApp As PowerPoint.Application, sld As Slide
Dim s As PowerPoint.Shape
Dim gChart As Chart, i As Integer
ppApp.Visible = True
i = 3
Set sld = ActivePresentation.Slides(i)
sld.Select
For Each s In ActivePresentation.Slides(i)
If s.Type = msoEmbeddedOLEObject Then
s.Select 'select the object
s.OLEFormat.Activate 'Activate it (like 2x click))
ActiveWindow.Selection.Unselect 'To let it close
ActiveWindow.View.GotoSlide s.Slideindex 'make current slide active
End If
Next s
End Sub
A macro that opens the workbooks to refresh the charts and automatically closes them would be great. Thank you!

Pasting Excel Chart into PowerPoint Slide

The below Sub is supposed to paste an Excel chart into a newly created PowerPoint slide. It then exports the chart as a PNG:
Sub ChartsToPowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
'Open PowerPoint and create an invisible new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Add(msoFalse)
'Set the charts and copy them to a new ppt slide
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.ChartArea.Copy
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
'Save Images as png
path = "C:\Users\xyz\Desktop\"
For j = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
.Export path & j & ".png", ppShapeFormatPNG
End With
Next j
pptApp.Quit
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
I get a Run-time error:
Shapes (unknown member): Invalid request. Clipboard is empty or contains data which may not be pasted here.
At the line:
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
Error http://im64.gulfup.com/pZNwxJ.png
I tried pptSlide.Shapes.Paste but it gives the same error.
When I amend pptApp.Presentations.Add(msoFalse) to pptApp.Presentations.Add only it works but the PowerPoint App is displayed.
When I change to .PasteSpecial DataType:=ppPasteEnhancedMetafile or .PasteSpecial DataType:=ppPastePNG everything runs smoothly even with .Add(msoFalse).
I am thinking it might be something to do with setting the focus or so.
PasteSpecial and CommandBars.ExecuteMso should both work (tested your code in Excel/PowerPoint 2010 with the following caveat:
When you add presentation, you have to open it WithWindow:=True
Set pptPres = pptApp.Presentations.Add(msoCTrue)
I did some more digging, you need to use the CopyPicture method and then I think you can open withwindow=False. Try:
Sub ChartsToPowerPoint()
Dim pptApp As PowerPoint.Application
Dim pptPres As PowerPoint.Presentation
Dim pptSlide As PowerPoint.Slide
Dim objChart As Chart
'Open PowerPoint and create an invisible new presentation.
Set pptApp = New PowerPoint.Application
Set pptPres = pptApp.Presentations.Add(msoFalse)
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.CopyPicture
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
'Save Images as png
Path = CreateObject("Wscript.Shell").SpecialFolders("Desktop") & "\"
For j = 1 To pptSlide.Shapes.Count
With pptSlide.Shapes(j)
.Export Path & j & ".png", ppShapeFormatPNG
End With
Next j
pptApp.Quit
Set pptSlide = Nothing
Set pptPres = Nothing
Set pptApp = Nothing
End Sub
This is a common error we can experience when we copy information from one Office application to another. The best way I can describe it is that the program runs too fast and the information we copy never actually makes it into our clipboard.
This means that when we go and try to paste it, we get an error because there is nothing in our clipboard to paste.
Now lucky for us there is a way to fix this error but it requires us to add an extra line of code.
'Copy the chart
Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
objChart.CopyPicture
'Pause the application for ONE SECOND
Application.Wait Now + #12:00:01 AM#
'Paste your content into a slide as the "Default Data Type"
Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
pptSlide.Shapes.PasteSpecial DataType:=ppPasteDefault, Link:=msoFalse
Now all I did was add an extra line of code that pauses your Excel application for one second. This will give it enough time to make sure that the information is stored in your clipboard.
You might be asking the question why does this happen sometimes but then not other times. Well, it just boils down to this the clipboard can act unpredictably and clear out information inside of it.
That's why if we can avoid storing the information in the clipboard we try to. However, in this case, we can't avoid it so we just have to live with the unpredictability.
#areed1192's answer might work if PowerPoint had an Application.Wait message, but that's an Excel thing.
I was able to do something similar by using a technique found here:
Which is to say, put a the top of the module:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
And then call it like this:
Sleep 1000
DoEvents
(I'm not positive the DoEvents helped, but it seemed like it might be a good idea for resolving a race condition in VBA if that's what's going on.)
sld.Shapes.PasteSpecial DataType:=0
or
sld.Shapes.PasteSpecial DataType:=ppPasteShape

Resources