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

时间:2017-03-09 04:50:51

标签: vba powerpoint

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

我是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

1 个答案:

答案 0 :(得分:0)

感谢您发布您的代码;另外,你应该解释一下究竟是什么问题。它不编译,它做错了什么,它没有做任何事情......?我假设这应该在幻灯片放映视图中运行,并且其中一个幻灯片上有一个文本框。这对你来说可能是显而易见的,但对整个世界来说并非如此。最好事先说清楚,为每个人节省一些时间。

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

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