【问题标题】:powerpoint search box vbaPowerPoint 搜索框 vba
【发布时间】:2014-12-22 14:31:29
【问题描述】:

我正在尝试在 powerpoint 中编写一些 vba 代码以在幻灯片中搜索一个单词,然后转到该幻灯片并以某种方式格式化该单词以使其突出。到目前为止,我已经添加了一个 activex 文本框并使用了以下代码:

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

    If KeyCode = 13 Then 'ENTER PRESSED
        If Me.TextBox1.Text <> "" Then
            For Each osld In ActivePresentation.Slides
                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.SlideIndex)
                                Me.TextBox1.Text = ""
                                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】:

这是一个示例,您可以从中挑选并使用以下部分:

Dim oSl As Slide
Dim oSh As Shape
Dim oTxtRng As TextRange
Dim sTextToFind As String

sTextToFind = "Text"

For Each oSl In ActivePresentation.Slides
    For Each oSh In oSl.Shapes
        If oSh.HasTextFrame Then
            If oSh.TextFrame.HasText Then
                If InStr(oSh.TextFrame.TextRange.Text, sTextToFind) > 0 Then
                    Set oTxtRng = oSh.TextFrame.TextRange.Characters(InStr(oSh.TextFrame.TextRange.Text, sTextToFind), Len(sTextToFind))
                    Debug.Print oTxtRng.Text
                    With oTxtRng
                        .Font.Bold = True
                    End With
                End If
            End If
        End If
    Next
Next

基本上,这个想法是找到文本(就像您已经完成的那样),然后获取一个代表找到的文本的范围,并设置适合的范围。在这种情况下,将其变为粗体。

【讨论】:

    【解决方案2】:

    感谢@steverindsberg

    如果有人想知道我最终使用了什么,我已将两个代码合并为:

    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 ActivePresentation.Slides
                 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.SlideIndex)
                             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
    

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多