2014-10-27 80 views
0

我想在PowerPoint中編碼一些vba來搜索幻燈片中的單詞,然後轉到該幻燈片並以某種方式格式化該單詞以使其突出顯示。到目前爲止,我有添加一個ActiveX文本框,用下面的代碼: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 

    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 

這工作正常上字查找幻燈片,但不格式化字。有任何想法嗎??

+0

我鼓勵你將代碼格式化爲更具可讀性,現在真的很難遵循現在的方式 – tkanzakic 2014-10-27 12:07:40

+0

感謝你爲Matt – edparker109 2014-10-27 13:45:45

回答

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 

基本上,這個想法是找到文本(如您已完成),然後獲取表示找到的文字範圍並設置適合的範圍。在這種情況下,將其轉爲粗體。

0

感謝@steverindsberg

如果有人想知道最後我用,我已經amalgamted兩碼爲:

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 

感謝您的幫助!