ActivatePreviousShape

PowerPoint allows one to insert VBA controls on the slides but it doesn't provide any way to set focus on a particular VBA control. The ActivateShape() subroutine available enables you to do just that. Once the shape has focus, pressing the Shift + Tab key doesn't move the focus to the previous shape. For shapes that can capture the Tab key (the TextBox VBA control is one of those controls), the ActivatePreviousShape() subroutine can help set the focus to the previous shape. Pass the currently focused VBA control shape as input to ActivatePreviousShape() and it will set the focus to the previous control.

Download the example presentation from hereDownload to see ActivateShape(), ActivateNextShape() and ActivatePreviousShape() in action.

Notes:

  • ActivatePreviousShape() will set the focus to the previous shape in the Z order (ZOrderPosition property).
  • ActivatePreviousShape() can work with any kind of ActiveX control in PowerPoint 97 and above.

See Also: ActivateShape(), ActivateNextShape()

Using ActivatePreviousShape():

The following example shows how to use ActivatePreviousShape(). It assumes that you have TextBox1 as a VBA TextBox control on Slide1. We write KeyUp handler for TextBox1 as follows:

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If (KeyCode = vbKeyTab) And (Shift = 1) Then
        ActivatePreviousShape Slide1.Shapes("TextBox1")
    End If
End Sub

Code for ActivatePreviousShape():


'
' Copyright (C) 2002 OfficeOne
'

Private Declare Sub keybd_event Lib "user32" ( _
    ByVal bVk As Byte, _
    ByVal bScan As Byte, _
    ByVal dwFlags As Long, _
    ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" ( _
    ByVal wCode As Long, _
    ByVal wMapType As Long) As Long

Private Const VK_SHIFT = &H10&
Private Const KEYEVENTF_KEYUP = &H2&

Sub ActivatePreviousShape(ByVal CurrentShp As Shape)
    Dim Sld As Slide
    Dim Shp As Shape
    Dim CurrentZPosition As Long
    Dim NextShp As Shape
    Dim NextZPosition As Long

    Set Sld = CurrentShp.Parent

    CurrentZPosition = CurrentShp.ZOrderPosition
    NextZPosition = 0
    For Each Shp In Sld.Shapes
        If Shp.Type = msoOLEControlObject Then
            If (Shp.ZOrderPosition < CurrentZPosition) And _
                (NextZPosition < Shp.ZOrderPosition) Then
                Set NextShp = Shp
                NextZPosition = NextShp.ZOrderPosition
            End If
        End If
    Next

    If NextShp Is Nothing Then
        For Each Shp In Sld.Shapes
            If Shp.Type = msoOLEControlObject Then
                If (Shp.ZOrderPosition > CurrentZPosition) And _
                    (NextZPosition < Shp.ZOrderPosition) Then
                    Set NextShp = Shp
                    NextZPosition = NextShp.ZOrderPosition
                End If
            End If
        Next
    End If

    If Not (NextShp Is Nothing) Then
        ActivateShape NextShp
        keybd_event VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0
    End If
End Sub

Contact OfficeOne on email at officeone@officeoneonline.com. Copyright © 2001-2023 OfficeOne. All rights reserved.