使用 VBA 在 PowerPoint 中添加搜索栏,搜索用户在特定幻灯片中输入的单词并突出显示它?

Adding Search Bar in PowerPoint using VBA which searches the word entered by the user in a particular slide and highlights it?

我想在我的演示文稿中添加一个搜索栏,用于搜索用户在特定幻灯片中输入的特定单词并将其突出显示。该词将附加一个超链接,该超链接将指向该词的含义。

我是VBA的新手。我有这段代码,需要进行必要的更改才能使其正常工作。

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim osld As Slide
Dim oshp As Shape
Dim b_found As Boolean
Dim oTxtRng As TextRange
Dim sTextToFind As String

sTextToFind = Me.TextBox1.Text


 If KeyCode = 13 Then 'ENTER PRESSED
 If Me.TextBox1.Text <> "" Then
     For Each osld In Application.ActivePresentation.Slides(5)
         For Each oshp In osld.Shapes
             If oshp.HasTextFrame Then
                 If oshp.TextFrame.HasText Then
                     If InStr(UCase(oshp.TextFrame.TextRange), UCase(Me.TextBox1.Text)) > 0 Then
                     SlideShowWindows(1).View.GotoSlide (osld.sectionIndex(5))
                     Set oTxtRng = oshp.TextFrame.TextRange.Characters(InStr(oshp.TextFrame.TextRange.Text, sTextToFind), Len(sTextToFind))
                     Debug.Print oTxtRng.Text
                     With oTxtRng
                      .Font.Bold = True
                 End With

             b_found = True
          Exit For
       End If
    End If
 End If
 Next oshp
 If b_found = True Then Exit For
 Next osld
 End If
 If b_found = False Then MsgBox "Not found"
End If
End Sub

感谢您发布代码;此外,您应该解释它到底是什么问题。它不编译,它做错事,它什么都不做......?我假设这应该在幻灯片放映视图中 运行,并且其中一张幻灯片上有一个文本框。这对您来说可能是显而易见的,但对整个世界而言却并非如此。最好先说清楚,省去大家的时间。

试一试。请注意,它只会在给定的文本范围内找到相关单词的第一个实例。

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim osld As Slide
Dim oshp As Shape
Dim b_found As Boolean
Dim oTxtRng As TextRange
Dim sTextToFind As String

'sTextToFind = Me.TextBox1.Text

 If KeyCode = 13 Then 'ENTER PRESSED
 If Me.TextBox1.Text <> "" Then
   sTextToFind = Me.TextBox1.Text
     'For Each osld In Application.ActivePresentation.Slides(5)
    For Each oshp In ActivePresentation.Slides(5).Shapes
        If oshp.HasTextFrame Then
            If oshp.TextFrame.HasText Then
                If InStr(UCase(oshp.TextFrame.TextRange), UCase(Me.TextBox1.Text)) > 0 Then
                    'SlideShowWindows(1).View.GotoSlide (osld.sectionIndex(5))
                    SlideShowWindows(1).View.GotoSlide (5)
                    Set oTxtRng = oshp.TextFrame.TextRange.Characters(InStr(oshp.TextFrame.TextRange.Text, sTextToFind), Len(sTextToFind))
                    Debug.Print oTxtRng.Text
                    With oTxtRng
                     .Font.Bold = True
                    End With

                    b_found = True
                    Exit For
                End If
            End If
        End If
    Next oshp
'    If b_found = True Then Exit For
'    Next osld
 End If
 If b_found = False Then MsgBox "Not found"
End If
End Sub