vba code for powerpoint - vba

I have an Excel sheet for sales KPIs that is linked to a SQL database and set to refresh every 5 minutes.
I built a PowerPoint that displays in our sales office. I copied the pie charts from Excel, using the Link Data option. When I first build the PowerPoint, this works awesome. Every 5 minutes, when the Excel data updates from SQL, the PowerPoint automatically updates.
However, when I close Excel and PowerPoint and open it the next day, the pie charts in PowerPoint no longer maintain a dynamic link. In other words, I have to manually click on every chart (there are over 15 of them) and click Refresh Data. Even after I do that, the link is no longer dynamic -- it won't auto-update when the Excel chart changes.
To be clear, I have both documents open when this is happening.
I'm assuming there's a way to do this with VBA and would like help to write the code. Once PP is open, I want it to continuously auto-update every time Excel changes. It's okay if Excel has to be open to make this work.
Appreciate any help.

This is untested, but the idea is that whenever the worksheet is updated (changed) then this should update the links in the powerpoint file called KPIs.pptx.
It may need to pause the macro until the PPTX file is updated, then release the object.
Private Sub Worksheet_Change(ByVal Target as Range)
Dim objPPT as Object
objPPT = GetObject("C:\username\KPIs.pptx")
objPPT.UpdateLinks
'may need a pause here
onjPPT = Nothing
End Sub

Related

VBA for Powerpoint: Change Slide in One Powerpoint by Selecting Button in Another

I am a beginner at VBA.
I am designing a somewhat interactive Powerpoint presentation/s. I want to be able to have three separate Powerpoint presentations open that will link together. I have been trying (without success) to create, in VBA, code which will change the current displayed slide on one Powerpoint file by clicking a button in another. I can hyperlink to the set slide but this causes this slide to pop up on the same screen in which it is clicked, despite it being already open in another screen (I don't want this).
Thanks in advance for any help,
Holly
VBA uses an object model that is a huge hierarchy of attributes and functions that represent the application. You can use this model to view and update attributes to get text, resize, and modify the application. You should look at some tutorials to get you started. When editing your code, you can press F2 to see and explore this object model. You can press F8 to run your code line by line (debug mode) and see what is happening.
To your question, you can access the open presentations in the application.presentations object (https://learn.microsoft.com/en-us/office/vba/api/powerpoint.presentations). You could then use a presentation in that list and use the ActiveWindow.View.goToSlide function (https://learn.microsoft.com/en-us/office/vba/api/powerpoint.view.gotoslide). Here is a free tutorial that I've used in my VBA journey (https://www.tutorialspoint.com/vba/index.htm).
PowerPoint has a Presentations collection that contains all currently open presentations. You can get a reference to any of them via Presentations("name") where "name" is the filename of the presentation, sans extension.
So ... assuming you've got three presentations open, a.pptx, b.pptx, c.pptx you can do something like this:
Sub SlideChange()
With Presentations("c")
.SlideShowWindow.View.GotoSlide (3)
End With
End Sub
If you run the above in any of the presentations, it will change the slide show window displaying presentation c to the third slide.

Hide PowerPoint presentation while VBA macro is running

I have this macro that copies excel tables and charts to power point. However the power point has to be visible for it to work. Unfortunately when it is visible and while the code is running and user touches the slides the program crashes.
I use this code to hide the presentation.
Set pres = Papp.presentatjons.open(path + pptfilename, with window:=msofalse).
It works, unfortunately this code (below) which is needed, needs the window or presentation slide to be open to copy charts and tables to the presentation.
Set ppslide = pres.slides(Papp.activewindow.selection.sliderange.slideindex)
Is there a code that substitute the second code so that I could hide the presentation window while running the program ?
Have a look at http://skp.mvps.org/ppt00033.htm
The code there will help you, but does not allow me to post a copy here

Is it possible to have my Excel table linked into PowerPoint update automatically in PPT when a change occurs in the Excel source?

So I copy a table in Excel > Paste Special > Paste Link > Hit OK and my table shows in my PPT slide
If I right click on my table, I can click update link, which will update the slide to reflect any changes in the spreadsheet
Problem is, the Excel table changes automatically, several times every hour, I can't be hitting the update link every time, any way to make the slide update automatically either if changes occur in the Excel table or every 2-3 minutes would work too? Using Excel and PPT 2013 by the way
Thanks in advance
Yes, there is a way:
ActivePresentation.UpdateLinks
will update all the links in your presentation.
Of course, if you have a specific presentation in mind, you can use that one instead of ActivePresentation.
However, I don't know any way to update links one-by-one without side effects. You could use
ActiveWindow.Selection.ShapeRange(1).OLEFormat.DoVerb wdOLEVerbPrimary
but this will open Excel Applications or Workbooks and there is no good way of Closing them and Hiding them while they are being used.

Selecting and switching to veryhidden Sheet not working properly

I have been writing a few macros lately to navigate around sheets / change visibility / import-export data etc. I normally just embed the macros as buttons on the sheets.
Normally this works well, however, I do keep experiencing an issue where with a macro, or a userform I unhide a veryhidden sheet, select it and exit the macro, or form.
I do this though via:
With Sheets("Sheet1")
.visibile = xlsheetvisible
.activate
.Range("A1").select
End With
When I then try to manipulate the sheet - e.g. type in a cell, delete data from cell, or insert / delete rows using the GUI rather than doing it via code, the operations happen on the original sheet with the button that called the form or macro. rather than the new one....
Is anyone else experiencing this? Am I doing something wrong?
Give worksheet object for cells while referring the editing
for ex... sheets().range().paste
or activate the sheet which you want to manipulate before your code (which manipulates the sheet)
Found the Solution!!!
Upon further investigation it seems like this is only broken in Office 2013. I tried it on Office 2010 and it worked fine.
The solution is to invoke the vbModeless command after Userform.show so that would be:
Userform1.Show vbModeless
Not perfect, if you want the user to dismiss the Userform before going back to the worksheet, but hey -it's a workaround :)
Let's hope Office 2016 will fix the bug (I'll be upgrading later this month)

Powerpoint VBA code to appear on all new documents

I have a macro in Powerpoint 2010 to open a new document based on a template based in a central location, to be installed on several machines.
Sub PowerpointTemplate()
Application.Presentations.Open ("file")
End Sub
Powerpoint does not save the macro. Even when setting "Macro in:" to "All open presentations", it seems to reset this selection to "Presentation 1". The macro works for the time that I have this current document open, but once I close Powerpoint and reopen it, the macro has been removed.
Is there anyway to set the macro to permanently apply to all presentations?
You need to create an Add-in and load it in order to make the code available to all open presentations.
Setting Macro In to All Open Presentations simply SHOWS you the available public macros in all open presentations; it has no effect on where the macro code is saved.