powerpoint搜索框vba

时间:2014-10-27 12:00:20

标签: vba powerpoint powerpoint-vba

我正在尝试在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

这适用于找到带有单词的幻灯片但不格式化单词。任何想法??

2 个答案:

答案 0 :(得分:0)

这是一个你可以分开并使用以下内容的例子:

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

基本上,我们的想法是找到文本(就像你已经做过的那样),然后得到一个代表找到的文本的范围,并设置适合的范围格式。在这种情况下,将其变为粗体。

答案 1 :(得分:0)

谢谢@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

感谢您的帮助!