【问题标题】:Adding Search Bar in PowerPoint using VBA which searches the word entered by the user in a particular slide and highlights it?使用 VBA 在 PowerPoint 中添加搜索栏,搜索用户在特定幻灯片中输入的单词并突出显示它?
【发布时间】:2017-03-09 04:50:51
【问题描述】:

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

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

【问题讨论】:

    标签: vba powerpoint


    【解决方案1】:

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

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

    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
    

    【讨论】:

    • 代码的问题是 For Each osld In Application.ActivePresentation.Slides(5) 显示错误。我想要的是假设幻灯片 5 中有 20 个单词,我希望此搜索栏位于幻灯片 1 中,并且用户可以使用它搜索这 20 个单词中的任何一个。
    • 是的,我认为这会引发错误。请参阅重写的版本。
    • 完美运行。谢谢你。如果我有任何其他疑问,有什么方法可以直接与您联系吗?
    • 最好再发一个帖子。我定期在这里检查。
    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 2011-12-20
    • 2015-10-14
    • 2014-02-15
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多