Here at BrightCarbon we’re always looking for new ways to improve our own PowerPoint productivity and then share that knowledge with the presentation community (that includes you, by the way!). One of the ways we do this is by using VBA code to automate and extend the functionality of PowerPoint.We publish free PowerPoint VBA code snippets here in our blog for you to use. This article explains how to grab the code from our articles and use it in your PowerPoint project, so that you can take your productivity to the next level!
What is VBA?
Visual Basic for Applications (VBA) is a programming environment for Microsoft Office applications. It’s included with your installation of Office by default (unless your system administrator has deactivated it). PowerPoint VBA provides you with a way to do one of two thingsusing macros and add-ins:
Automate PowerPoint:If you ever find yourself repeating the same task over and over again, VBA could be your new best friend. Let’s say you have 100 slides and you need to unhide all hidden objects across all those slides. That could take you many eye-straining minutes, but with a PowerPoint VBA it takes around a second.
Extend PowerPoint:Sometimes PowerPoint doesn’t have the feature you need to complete your task. As an example, if you end up deleting default layouts from a template, there’s no easy way in PowerPoint to get them back. This article includes PowerPoint VBA code to do just that!
How to open the VBE (Visual Basic Editor)
Getting to meet your VBA friend is very simple. With PowerPoint open and at least one presentation file open, press Alt+F11* on your keyboard. This will open the VBE (Visual Basic Editor):
*If for some reason Alt+F11 isn’t mapped on your keyboard you can right click anywhere on the ribbon, select Customize the Ribbon… and in the window that appears, tick the Developer Tab check box over on the right hand side before clicking OK to close the window. Now you can click the Visual Basic button within this tab:
Adding PowerPoint VBA code
To add some VBA code, you need a container to put it in so go ahead and click Insert from the menu and then select Module:
You now have a module ready to paste the VBA code into from one of our blog articles:
Copy the VBA code from the required blog article by double-clicking on it and then paste it into the Module1 window above. Here’s a very simple example of some code to display a message dialogue:
'----------------------------------------------------------------------------------
' PowerPoint VBA Macro to display Hello World message.
'----------------------------------------------------------------------------------
' Copyright (c) 2019 BrightCarbon Ltd. All Rights Reserved.
' Source code is provided under Creative Commons Attribution License
' This means you must give credit for our original creation in the following form:
' "Includes code created by BrightCarbon Ltd. (brightcarbon.com)"
' Commons Deed @ http://creativecommons.org/licenses/by/3.0/
' License Legal @ http://creativecommons.org/licenses/by/3.0/legalcode
'----------------------------------------------------------------------------------
' Purpose : Displays a dialog box with a Hello World text message.
' Author : Jamie Garroch
' Date : 06MAY2019
' Website : https://brightcarbon.com/
'----------------------------------------------------------------------------------
Sub HelloWorld()
MsgBox "Hello World!", vbInformation + vbOKOnly, "This is my first VBA Macro"
End Sub
You should now see something like this:
Because this code is just a single Sub procedure called HelloWorld, it’s referred to as a macro.
Running the PowerPoint VBA macro
Now you have the macro in your presentation you can use Alt+Tab to return to the more familiar PowerPoint window. From here, the macro can be run by pressing Alt+F8 on your keyboard (or by clicking the Macros button in the Developer tab) which opens a window containing a list of available macros:
Saving your file
With the macro(s) in your presentation file you can nowuse it in that file or with any other PowerPoint file you have open. Bear in mind that to use your macros, the file that contains them must be open.You can add as many modules and macros as you like in a PowerPoint file so you could create your own library of macros in a single file that you then access from all other decks. When you have multiple PowerPoint files open, make sure you select the file that contains your macros in the Macro window by clicking the Macro in drop down:
Once you’ve added VBA code to your presentation, PowerPoint will ask you to save it as a pptm file (the ‘m’ stands for macro) instead of the more familiar pptx format. You can go ahead and do this to either keep an archive copy of your code-enabled project orto create your personal macro library.
If you want to distribute your presentation,it’s advisable tosave it using the familiar pptx format so that your recipients don’t see lots of verbose security messages when opening pptm files!
You can make your file saveable as a standard presentation again by right–clicking on eachcode module in the project explorer pane, clicking RemoveModuleX…and either click Yes(if you want to keep a backup of the modules independently of your presentation) or Nowhen asked if you want to save the module before removing it:
Now your presentation doesn’t include any code and you can save it as a pptx file.
So, there you have it. You now know how to open the VBE, insert a PowerPoint VBA code module, paste code into it, run the macro and save the file in either pptm or pptx formats. All you need is a cool macro to make your daily life even easier. Keep checking in with our blog for more useful macros – like this one on restoring default slide master layouts! And let us know if there’s any particular macros you’d like to see by commenting below.
Pictures in PowerPoint can be tricky to get to grips with. Choosing the right size has an impact on both the file size of your presentation and the maximum monitor/projection size you can use without degrading quality.
How do you make sure the presentations, spreadsheets and documents your create in Microsoft Office are kept in line with your brand? Learn how to create, save and deploy a custom Office theme across PowerPoint, Excel and Word.
I am trying to make a ppt file that loops until stopped. then I save it as a video. the ppt ran and looped continuously. Once recorded as video it stopped looping. do you have code to make ppt work when in video format
Hi Charles. As soon as you export a PowerPoint deck as a video all the PowerPoint functionality is removed as the file is magically transformed into an MP4 file, without VBA (sob sob). The only way to make the video loop is to use the looping feature of your video player.
Hi Jamie, thanks for the clear into, I am very new to this so that really helps. I am trying to develop a VBA macro that looks for the left hand mouse key being pressed and held down for more than two seconds whilst over a shape in slideshow mode. Once this is satisfied (i.e. two second press) for it then to hyperlink or take the user to a specified slide or even the next slide worst case.
I realise there is an automated/ built in feature (Action) that does this type of thing for a mouse click or mouse over but I really need a “long press” to activate if possible.
Hi Simon and thanks for a great question. What you’re looking to do is pretty complex because VBA doesn’t natively support mouse actions in the PowerPoint slide show window. But, it is possible to use a Windows API (hence no Mac compatibility) called GetAsyncKeyState to gain access to mouse button click events. I had a look at this and quickly ran into a brick wall because an action link to a macro in slide show mode (Insert / Action / Mouse Click / Run macro) fires on the mouse up event, not mouse down. That means any corresponding VBA timer code can’t run until after the user releases the button and hence too late to detect if it was held down for two seconds. Maybe something could be done with the mouse over event to simulate what you need to achieve? Another approach could be to use the mouse down event on an invisible userform although that is also getting very involved with multiple Windows APIs. Depending on what you’re trying to do, you could also start the timer on click one, change the colour of the clicked shape and show countdown text before reverting to the original colour. If the user clicks a second time before the time expires, then the hyperlink is fired.
I have tried using your randomizing macro with a powerpoint – I must be doing something wrong, because it isn’t putting the slides in random order. Please advise! I copied the macro exactly (using cut & paste), and thought I was following all the directions here for how to use it in the powerpoint. But, no random presentation of the slides. Boo hoo!
Hi Marya. Let’s check that VBA is installed and enabled on your machine. Can you add the following macro to the VBE project (just below the existing one) and try to run it from the PowerPoint window using Alt+F8?
I am trying to format my title page so that the number displayed is equal to the linked slide and updates automatically wherever the slide is moved. For example “about us” is on slide #5 and linked, so it goes to slide 5 when you click on the word. I need the number (in a separate text box) to update automatically to the slide number location that the link goes to.
Hi Mary and thanks for the question. It looks like you’re interested in some kind of automated agenda slide builder. That’s a fair bit of code to create and quite complex as it needs to handle events from PowerPoint to detect when slides have moved. It could be possible to write a simpler macro which you run manually each time you want to update that title page. You’d need start by finding a way to identify which objects are your numerical indicators. For example, if you named your objects in the selection pane (Alt+F10) “Agenda Link”, then is simple macro could be a starting place for you: Sub UpdateAgendaNumbers() Dim oSld As Slide Dim oShp As Shape Dim LinkedSlideIndex As Long On Error Resume Next For Each oSld In ActivePresentation.Slides For Each oShp In oSld.Shapes If oShp.Name = “Agenda Link” Then If oShp.ActionSettings(ppMouseClick).Action = ppActionHyperlink Then If oShp.HasTextFrame Then LinkedSlideIndex = Split(oShp.ActionSettings(ppMouseClick).Hyperlink.SubAddress, “,”)(1) oShp.TextFrame.TextRange.Text = LinkedSlideIndex End If End If End If Next Next End Sub
Great wealth of information. Have never used macros before but was looking to use them to help with this situation. At work we use Work Orders (created in Power Point) and are looking to include a sequential number to them (print 50-100 copies of one slide with the numbers) and if possible would like the number to continue from the last printed number…been trying to find some code to help but not having much luck possible partly due to being new to macros
That’s definitely something we could help design for you Joshua. If you’d like to discuss further, please click the Contact button at the top of this page.
and it works just fine. But, when I move the mouse off the text box, onto the invisible rectangle with this code attached to the mouseover event, it doesn’t change the text color back to it’s original color and remains the color I changed it to mentioned above. I know the mouseover event is being triggered because I checked “Highlight when mouse over” and I am seeing the highlight on the invisible rectangle:
Public Sub ResetGraphicHover(ByRef oCover As Shape) Dim oSld As Slide Dim oShp As Shape Set oSld = oCover.Parent For Each oShp In oSld.Shapes With oShp.TextFrame.TextRange.Font.Color If .RGB = RGB(0, 130, 202) Then .RGB = RGB(121, 135, 156) End With Next End Sub
Hi Dave. I took your code and it works for me. You could add a debug line after the For Each… line in the rest macro to check that (a) it’s firing and (b) which shapes are being looked at on your slide. To do that, add this:
Debug.Print oShp.Name
After you run the slide show, check the output in the VBE Immediate pane (Ctrl+G to toggle it).
Hi I am creating an interactive game (matching cards or concentration) in PowerPoint. If the 2 cards match, I need a pop-up text box to appear. If the 2 cards do not match, I need a sound to play.
I understand I need programming to make this happen. Please help or give alternative ways to achieve this. Thanks.
Hi Producer I will like to get comments on macros you can make available to me. Beautiful. I am using this approach frequently to make offline projects. Thanks. S. Fas
There is absolutely no doubt that the BrightCarbon presentation was a quantum leap beyond anything else at the conference with respect to the clarity of the presentation.
very simple, very explicit, very good help for a beginner vba programmer in powerpoint. Thanks
great resource, thanks. I’ve used VBA for years in MSaccess, and this is a good refresher for me.
I am trying to make a ppt file that loops until stopped. then I save it as a video. the ppt ran and looped continuously. Once recorded as video it stopped looping. do you have code to make ppt work when in video format
Hi Charles. As soon as you export a PowerPoint deck as a video all the PowerPoint functionality is removed as the file is magically transformed into an MP4 file, without VBA (sob sob). The only way to make the video loop is to use the looping feature of your video player.
Yeah, your best off recording a screen capture of the presentation running, then cutting it so it loops perfectly.
Hi Jamie, thanks for the clear into, I am very new to this so that really helps. I am trying to develop a VBA macro that looks for the left hand mouse key being pressed and held down for more than two seconds whilst over a shape in slideshow mode. Once this is satisfied (i.e. two second press) for it then to hyperlink or take the user to a specified slide or even the next slide worst case.
I realise there is an automated/ built in feature (Action) that does this type of thing for a mouse click or mouse over but I really need a “long press” to activate if possible.
Any help appreciated.
Hi Simon and thanks for a great question. What you’re looking to do is pretty complex because VBA doesn’t natively support mouse actions in the PowerPoint slide show window. But, it is possible to use a Windows API (hence no Mac compatibility) called GetAsyncKeyState to gain access to mouse button click events. I had a look at this and quickly ran into a brick wall because an action link to a macro in slide show mode (Insert / Action / Mouse Click / Run macro) fires on the mouse up event, not mouse down. That means any corresponding VBA timer code can’t run until after the user releases the button and hence too late to detect if it was held down for two seconds. Maybe something could be done with the mouse over event to simulate what you need to achieve? Another approach could be to use the mouse down event on an invisible userform although that is also getting very involved with multiple Windows APIs. Depending on what you’re trying to do, you could also start the timer on click one, change the colour of the clicked shape and show countdown text before reverting to the original colour. If the user clicks a second time before the time expires, then the hyperlink is fired.
Valuable app
Hello I have a question:
Private Sub CommandButton2_Click()
ActivePresentation.FollowHyperlink _
Address:=”http://192.168.16.49/?OUT1=ON”, _
NewWindow:=False, AddHistory:=False
ActivePresentation.SlideShowWindow.View.GotoSlide (2)
End Sub
Now it opens Chrome. but how can i make it that it opens te address en afther that shut down chrome.
Hi Tom. Your example should open the default browser at the URL specified by the Address parameter. For more information on the FollowHyperlink method, see this Microsoft documentation: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.followhyperlink
Thank you very much! It’s exactly what I needed.
Hello,
I have tried using your randomizing macro with a powerpoint – I must be doing something wrong, because it isn’t putting the slides in random order. Please advise! I copied the macro exactly (using cut & paste), and thought I was following all the directions here for how to use it in the powerpoint. But, no random presentation of the slides. Boo hoo!
Hi Marya. Let’s check that VBA is installed and enabled on your machine. Can you add the following macro to the VBE project (just below the existing one) and try to run it from the PowerPoint window using Alt+F8?
Sub CheckVBA()
MsgBox “it’s working”
End Sub
Make sure the quotes are the straight type.
Hi Jamie,
I am trying to format my title page so that the number displayed is equal to the linked slide and updates automatically wherever the slide is moved. For example “about us” is on slide #5 and linked, so it goes to slide 5 when you click on the word. I need the number (in a separate text box) to update automatically to the slide number location that the link goes to.
Hi Mary and thanks for the question. It looks like you’re interested in some kind of automated agenda slide builder. That’s a fair bit of code to create and quite complex as it needs to handle events from PowerPoint to detect when slides have moved. It could be possible to write a simpler macro which you run manually each time you want to update that title page. You’d need start by finding a way to identify which objects are your numerical indicators. For example, if you named your objects in the selection pane (Alt+F10) “Agenda Link”, then is simple macro could be a starting place for you:
Sub UpdateAgendaNumbers()
Dim oSld As Slide
Dim oShp As Shape
Dim LinkedSlideIndex As Long
On Error Resume Next
For Each oSld In ActivePresentation.Slides
For Each oShp In oSld.Shapes
If oShp.Name = “Agenda Link” Then
If oShp.ActionSettings(ppMouseClick).Action = ppActionHyperlink Then
If oShp.HasTextFrame Then
LinkedSlideIndex = Split(oShp.ActionSettings(ppMouseClick).Hyperlink.SubAddress, “,”)(1)
oShp.TextFrame.TextRange.Text = LinkedSlideIndex
End If
End If
End If
Next
Next
End Sub
Great wealth of information. Have never used macros before but was looking to use them to help with this situation. At work we use Work Orders (created in Power Point) and are looking to include a sequential number to them (print 50-100 copies of one slide with the numbers) and if possible would like the number to continue from the last printed number…been trying to find some code to help but not having much luck possible partly due to being new to macros
That’s definitely something we could help design for you Joshua. If you’d like to discuss further, please click the Contact button at the top of this page.
I tried this changing the font color of text within the textbox. I used this to change the font color on a mouse over:
Public Sub GraphicHover(ByRef oGraphic As Shape)
oGraphic.TextFrame.TextRange.Font.Color.RGB = RGB(0, 130, 202)
End Sub
and it works just fine. But, when I move the mouse off the text box, onto the invisible rectangle with this code attached to the mouseover event, it doesn’t change the text color back to it’s original color and remains the color I changed it to mentioned above. I know the mouseover event is being triggered because I checked “Highlight when mouse over” and I am seeing the highlight on the invisible rectangle:
Public Sub ResetGraphicHover(ByRef oCover As Shape)
Dim oSld As Slide
Dim oShp As Shape
Set oSld = oCover.Parent
For Each oShp In oSld.Shapes
With oShp.TextFrame.TextRange.Font.Color
If .RGB = RGB(0, 130, 202) Then .RGB = RGB(121, 135, 156)
End With
Next
End Sub
Any clue where my ResetGraphicHover is failing?
Hi Dave. I took your code and it works for me. You could add a debug line after the For Each… line in the rest macro to check that (a) it’s firing and (b) which shapes are being looked at on your slide. To do that, add this:
Debug.Print oShp.Name
After you run the slide show, check the output in the VBE Immediate pane (Ctrl+G to toggle it).
Hi I am creating an interactive game (matching cards or concentration) in PowerPoint. If the 2 cards match, I need a pop-up text box to appear. If the 2 cards do not match, I need a sound to play.
I understand I need programming to make this happen. Please help or give alternative ways to achieve this. Thanks.
Hi Producer
I will like to get comments on macros you can make available to me. Beautiful.
I am using this approach frequently to make offline projects.
Thanks.
S. Fas
Excellent!!!
Thank you!