none
Refocusing on a VBA textbox on a PowerPoint Slide RRS feed

  • Question

  • I have a PowerPoint slide that contains one VBA texbox which receives the focus as soon as the slide opens.  However, the slide also contains other VBA command buttons.  When I click on a command button, the textbox is no longer active.  In other words, it has lost its ability to receive input.  Is there a way to have the textbox maintain its focus after clicking on one of the command buttons?
    Thursday, March 9, 2017 1:47 PM

All replies

  • Hi Oxidation888,

    generally we use Set Focus to activate the textbox.

    but in powerpoint you can see that Set Focus is not available.

    so I try to find any other way.

    you had mentioned that Textbox should not lost it focus.

    for that requirement , I find a code example you can refer below.

    '
     ' 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 ActivateNextShape(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 = 2 ^ 31 - 1
         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
             keybd_event VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_KEYUP, 0
         End If
    End Sub 


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

    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  tab key doesn't move the focus to the next shape. For shapes that can capture  the Tab key (the TextBox VBA control is one of those controls), the ActivateNextShape() subroutine can help set the focus to the next shape. Pass the currently focused  VBA control shape as input to ActivateNextShape() and it will set the focus to  the next control.

    Reference:

    ActivateNextShape() subroutine

    Demo Example Powerpoint File

    I try to test it on my side and find that Textbox is not losing the focus. so it can solve your issue.

    you can try to understand the code and you need to modify the code to work as per your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 10, 2017 1:36 AM
    Moderator
  • Thank you for all the advice.  I actually stumbled across a solution.  To prevent a command button from taking the focus away from the textbox, all I had to do was open the properties window of the command button and uncheck the option to "Take focus on click."  

    Thanks again!

    Sunday, March 12, 2017 9:34 PM
  • Hi Oxidation888,

    from your last reply I can see that you find the solution by yourself.

    this thread is still open.

    please try to mark your last post as an answer , so that we can close this thread.

    if you do not mark the answer then this thread will remain open.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 13, 2017 4:38 AM
    Moderator