How can I use PowerPoint VBA to assign [Alt-Tab] to an action button.

Is it possible to assign the [Alt+Tab] keyboard shortcut to an action button in PowerPoint, and if so can you show me how to write the VBA?

Thank you in advance for your help,
   Jon
Jon BredensteinerProject ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JSRWilsonCommented:
The only way would be to use SendKeys but it is unlikely to be very reliable. Maybe if you explained WHY you need the button to send Alt Tab there would be another way.

Sub ALTTAB()
    Application.SendKeys ("%{TAB}")
    DoEvents
End Sub
Jon BredensteinerProject ManagerAuthor Commented:
Thank you for your help...

I tried the code you provided, but I received the following error:

"
Compile error:
Method or data member not found
"

and it highlighted the "SendKeys" in the code.

I am trying to use an Excel file as a Dashboard to run four large PowerPoint files (around 75 slides each).  I would like the user to be able to open the Excel file from anywhere, and then use hyperlinks in the Excel file to navigate to custom slide shows in the various .ppsm files.

The problem is that if I use an action button to close the slideshow every time the user wants to go back to the dashboard the .ppsm files will completely close, and thus have to be reopened (over the network) every time a link on the dashboard is selected.

I cannot simply use a hyperlink in the .ppsm files to navigate back to the Excel file either, because we are trying to make the Excel file portable, so it can be distributed via emailed.

Let me know if you need more information, and thanks again for your assistance.
Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
If you need that level of control, you could try saving as a .pptm file and then control the start/stop/close actions of each presentation:

' Start a slide show
Public appPPT as Object
Public objPres as Object

' Start a slide show
' Assign macro to relevant action button
Sub StartSlideShow()
  Const FilePathAndName = "C:\Temp\example.pptm"
  ' Create an instance of PowerPoint
  Set appPPT = CreateObject("Application.PowerPoint")
  ' Open a given PowerPoint file
  Set objPres = appPPT.Open(FilePathAndName, msoTrue, msoFalse, msoTrue)
  ' Run a slide show
  appPPT.Presentations(1).SlideShowSettings.Run
Exit Sub

' Exit the Slide Show
' Assign macro to relevant action button
Sub ExitSlideShow(objSSW as Object)
  appPPT.SlideShowWindows(1).View.Exit
Exit Sub

' Close a presentation
Sub ClosePres()
  objPres.Close
End Sub

' Tidy Up (run when closing Excel VBA project)
Sub Tidy()
  Set objPres = Nothing
  Set appPPT = Nothing
End Sub

Open in new window


If you use the SendKeys method, you will probably run into issues as you won't know which window is open and if another window is opened by the user/system, this will only compound the problem. As John says, I would stay well clear of mimicking key strokes in such a case.

But, if you really want to go down this path, SendKeys is a method of the application when running in the Excel VBE and a method of VBA when running in PowerPoint. So, depending on which app is hosting your code, you would need this:

' Excel
Sub ALTTAB()
    Application.SendKeys ("%{TAB}")
    DoEvents
End Sub

' PowerPoint
Sub ALTTAB()
    VBA.SendKeys ("%{TAB}")
    ' Which is the same as:
    SendKeys ("%{TAB}")
    DoEvents
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

JSRWilsonCommented:
I'm wondering why you are using Excel as the dashboard. Wouldn't it be easier to create the dashboard in PPT?
Jon BredensteinerProject ManagerAuthor Commented:
Sorry guys, I've been out of the office sick this this week, so I have some catching up to do, but I haven't forgotten about this question.  I will respond ASAP.

Thank you both for your help,
   Jon
Jon BredensteinerProject ManagerAuthor Commented:
Okay, Jamie's SendKeys macro worked

Sub AltTab()
    SendKeys ("%{TAB}")
    DoEvents
End Sub

Open in new window


I tried your other code Jamie, but I could not get it to work.  I wasn't sure exactly how to use it, so I tried it in a .pptm file, assigning action buttons to each of the macros, but it didn't seem to do anything.

I also tried putting the code into Excel, and it caused an error saying it expected an End Sub, and highlighted the following line

' Assign macro to relevant action button

Anyway, you answered my question Jamie, so I'll definitely assign you the points; however, I do see what you were both saying about not mimicking key strokes, so I'll probably find another solution to my problem.
Jon BredensteinerProject ManagerAuthor Commented:
Sub AltTab()
    SendKeys ("%{TAB}")
    DoEvents
End Sub
Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
Sorry my original code has two errors in it. Both occurrences of Exit Sub should in fact be End Sub. I can't seem to edit the comment now.
Jon BredensteinerProject ManagerAuthor Commented:
Thanks Jamie,

Is the below code supposed to go in a .pptm file or a .xlsm file?  I'm pretty sure .xlsm is correct.


' Start a slide show
Public appPPT as Object
Public objPres as Object

' Start a slide show
' Assign macro to relevant action button
Sub StartSlideShow()
  Const FilePathAndName = "C:\Temp\example.pptm"
  ' Create an instance of PowerPoint
  Set appPPT = CreateObject("Application.PowerPoint")
  ' Open a given PowerPoint file
  Set objPres = appPPT.Open(FilePathAndName, msoTrue, msoFalse, msoTrue)
  ' Run a slide show
  appPPT.Presentations(1).SlideShowSettings.Run
End Sub

' Exit the Slide Show
' Assign macro to relevant action button
Sub ExitSlideShow(objSSW as Object)
  appPPT.SlideShowWindows(1).View.Exit
End Sub

' Close a presentation
Sub ClosePres()
  objPres.Close
End Sub

' Tidy Up (run when closing Excel VBA project)
Sub Tidy()
  Set objPres = Nothing
  Set appPPT = Nothing
End Sub

Open in new window

Jamie GarrochSenior Technical Consultant at BrightCarbonCommented:
I hadn't checked the code but have now. This works:

Option Explicit

' Macros for opening a presentation, starting and stopping a slide show
' Runs from any VBE, including Excel (uses late binding)

' Define late bound objects for PowerPoint application instance and presentation
Public appPPT As Object
Public objPres As Object

' Start a slide show
' Set FilePathAndName to the presentation file to be opened
' Assign macro to relevant action button
Public Sub StartSlideShow()
  Dim FilePathAndName As String
  FilePathAndName = "c:\temp\test.pptx"
  ' Create an instance of PowerPoint
  Set appPPT = CreateObject("PowerPoint.Application")
  ' Open the PowerPoint file specified by the string variable FilePathAndName
  Set objPres = appPPT.Presentations.Open(FilePathAndName, ReadOnly:=msoFalse, Untitled:=msoFalse, WithWindow:=msoFalse)
  ' Run the slide show
  objPres.SlideShowSettings.Run
End Sub

' Exit the specified Slide Show or the first one if none specified
' Assign macro to relevant action button
Public Sub ExitSlideShow(Optional objSSW As Object)
  If objSSW Is Nothing Then
    appPPT.SlideShowWindows(1).View.exit
  Else
    objSSW.View.exit
  End If
End Sub

' Tidy Up (run when closing Excel VBA project)
Public Sub Tidy()
  Set objPres = Nothing
  Set appPPT = Nothing
End Sub

Open in new window

Jon BredensteinerProject ManagerAuthor Commented:
It almost worked :)  The procedure [StartSlideShow] works to open a presentation in Slide Show mode, but the procedure [ExitSlideShow] is not available to run as a macro.

I opened a new question though, so you can also receive credit for helping me with this problem.

How can one control the start/stop/close actions of PowerPoint presentations from Excel or from another presentation?

Thanks again,
   Jon
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft PowerPoint

From novice to tech pro — start learning today.