I have to produce a report automatically based on data in Excel. The report has links (text boxes created using "paste as link") already set up and that need updating every time the code is run. I have the below code but it does not update the links. How is this possible?
Dim PowerPointApp As PowerPoint.Application
Set PowerPointApp = CreateObject("PowerPoint.Application")
Dim PowerPoint As PowerPoint.Presentation
PowerPointApp.Presentations.Open ("X:\Intranet\Templates\Investment Proposal Templates\IP Normal Template.pptx")
For Each sld In ActivePresentation.Slides
For Each sh In sld.Shapes
If sh.Type = msoLinkedOLEObject Then
sh.LinkFormat.Update
End If
Next
Next
Related
I have a PowerPoint presentation containing several charts. Each of these charts have an embedded PowerPoint Excel file behind the chart. The embedded Excel file source its data from an Excel (Master) file. When I make any changes in the Excel (Master) file these changes are not automatically displayed in the chart. In order to get the change reflected in the chart I need to right click the chart and then choose "Edit data". I found below code which helps me to automatically open all the embedded Excel files in the presentation.
My question is now, if I do not want the macro to open all the embedded Excel files but perhaps only a specific chart (chart 1). If so, how should I then re-write the code so it only open the embedded Excel file for Chart 1?
Update PowerPoint chart using VBA
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 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
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!
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
I have to post a lot of Excel charts to a specific PowerPoint document and I'm building out a macro in Excel VBA to do it for me.
I'm able to correctly open the PowerPoint presentation that I want to update, however I don't know how to set the presentation I just opened to a variable called MyPresentation.
Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application
PowerPointApp.Presentations.Open Filename:="obscured filepath and name"`
Obviously there's some additional code, but I'm trying to set the Presentation I just opened in line 3 set to the MyPresentation variable so I can reference the document I just opened.
I ended up finding a solution by the MVP Andy Pope.
Some relevant code snippets for future users. (FYI My PPT was already visible when I ran into the problem)
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
'Easier to define manually set links up front so it's easier to change/modify
DestinationPPT = "C:\yourfilepath\yourfilename.pptx"`
Lookup the Spreadsheet Guru's guide to opening PPT from Excel VBA
Then:
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
First you have to pave the way for using ppt files,
what I did:
Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Set PowerPointApp = CreateObject("PowerPoint.Application")
DestinationPPT = "path"
PowerPointApp.Presentations.Open (DestinationPPT)
I was trying to create an excel file that can generate a predefined powerpoint presentation on the basis of a specific worksheet, with different values within the excel-file.
My issue was that every person in my department should be able to download the excel-file and run it.
Defining Presentations within vba doesn't work for everyone since the reference that is required for this action isn't activated on every computer/excel-programm.
Createobject did the trick for me to get around the problem.
Here the code i am talking about:
Dim pptApp as object
Dim strpath as string
strpath = ThisWorkbook.Path & "\Test_Dummy.pptx"
Set pptApp = createobject("Powerpoint.application")
pptApp.Presentations.Open Filename:=strpath, readonly:=false, untitled:=true, withwindow:=true
What didn't work was stuff like:
Dim pptApp as Powerpoint.Application
or
Dim pptApp as object
Set pptApp = New Powerpoint.Application
without setting the required references in vba
This one worked for me:
'devlare variables
Dim MyPPT As Object
'create PowerPoint
Set MyPPT = CreateObject("Powerpoint.application")
'make it visible
MyPPT.Visible = True
'path to powerpoint
MyPPT.presentations.Open "path\powerpoint.pptx"
Set visible to true, before open it. otherwise it did not worked for me