2013-04-05 2 views
2

이 사이트의 일부 코드를 사용하여 Word 문서에서 키워드 검색을 수행하고 결과를 강조 표시하는 매크로를 만들었습니다.MS PowerPoint에서 텍스트 찾기 및 강조 표시

PowerPoint에서 효과를 복제하고 싶습니다.

다음은 Word 용 코드입니다.

Sub HighlightKeywords() 

Dim range As range 
Dim i As Long 
Dim TargetList 

TargetList = Array("keyword", "second", "third", "etc") ' array of terms to search for 

For i = 0 To UBound(TargetList) ' for the length of the array 

    Set range = ActiveDocument.range 

    With range.Find ' find text withing the range "active document" 
    .Text = TargetList(i) ' that has the words from the array TargetList 
    .Format = True ' with the same format 
    .MatchCase = False ' and is case insensitive 
    .MatchWholeWord = True ' and is not part of a larger word 
    .MatchAllWordForms = False ' and DO NOT search for all permutations of the word 

    Do While .Execute(Forward:=True) 
    range.HighlightColorIndex = wdYellow ' highlight the keywords from the for loop yellow 

    Loop 

    End With 
Next 

End Sub 

여기는 지금까지 PowerPoint에서 가지고있는 기능입니다. 작동하지 않습니다.

Sub HighlightKeywords() 

Dim range As range 
Dim i As Long 
Dim TargetList 

TargetList = Array("keyword", "second", "third", "etc") ' array of terms to search for 

For Each sld In Application.ActivePresentation.Slides 

For Each shp In sld.Shapes 

    If shp.HasTextFrame Then 

     Set txtRng = shp.TextFrame.TextRange 

For i = 0 To UBound(TargetList) ' for the length of the array 

    With range.txtRng ' find text withing the range "shape, text frame, text range" 
    .Text = TargetList(i) ' that has the words from the array TargetList 
    .Format = True ' with the same format 
    .MatchCase = False ' and is case insensitive 
    .MatchWholeWord = True ' and is not part of a larger word 
    .MatchAllWordForms = False ' and DO NOT search for all permutations of the word 

    Do While .Execute(Forward:=True) 
    range.HighlightColorIndex = wdYellow ' highlight the keywords from the for loop yellow 

    Loop 

    End With 
Next 

End Sub 

은 내가 MSDN을 통해 내 대답을 찾는 결국,하지만 나는 사람들이 제출 한 것과 같은 올바른 선택 대답에 매우 근접했다.

Sub Keywords() 

Dim TargetList 
Dim element As Variant 

TargetList = Array("First", "Second", "Third", "Etc") 

For Each element In TargetList 
    For Each sld In Application.ActivePresentation.Slides 
     For Each shp In sld.Shapes 
     If shp.HasTextFrame Then 
      Set txtRng = shp.TextFrame.TextRange 
      Set foundText = txtRng.Find(FindWhat:=element, MatchCase:=False, WholeWords:=True) 
      Do While Not (foundText Is Nothing) 
       With foundText 
        .Font.Bold = True 
        .Font.Color.RGB = RGB(255, 0, 0) 
       End With 
      Loop 
     End If 
     Next 
    Next 
Next element 

End Sub 

그 코드가 작동하지만 성능 악몽 밝혀 : 여기

내가 갔다 코드입니다. 아래에서 정답으로 선택한 코드는 훨씬 부드럽게 실행됩니다. 선택한 답변과 일치하도록 프로그램을 조정했습니다.

답변

2

AFAIK 묻어나는 단어은 (는) 에 강조 표시되어 있습니다. 당신은 직사각형 모양을 만들고 발견 된 텍스트 뒤에 놓고 색칠하는 방법을 벗어날 수 있습니다. 그러나 그것은 다른 볼 게임입니다.

다음은 모든 슬라이드에서 텍스트를 검색 한 다음 발견 된 텍스트를 굵게, 밑줄 및 전 범위로 만드는 예제입니다. 원하는 경우 글꼴의 색상을 변경할 수도 있습니다.

의 우리가 그것을 시도 후 모듈에이 코드를 붙여 넣기이

enter image description here

처럼 보이는 슬라이드 있다고 가정 해 봅시다. 나는 당신이 그것을 이해하는데 문제가 없도록 코드에 주석을 달았습니다.

Option Explicit 

Sub HighlightKeywords() 
    Dim sld As Slide 
    Dim shp As Shape 
    Dim txtRng As TextRange, rngFound As TextRange 
    Dim i As Long, n As Long 
    Dim TargetList 

    '~~> Array of terms to search for 
    TargetList = Array("keyword", "second", "third", "etc") 

    '~~> Loop through each slide 
    For Each sld In Application.ActivePresentation.Slides 
     '~~> Loop through each shape 
     For Each shp In sld.Shapes 
      '~~> Check if it has text 
      If shp.HasTextFrame Then 
       Set txtRng = shp.TextFrame.TextRange 

       For i = 0 To UBound(TargetList) 
        '~~> Find the text 
        Set rngFound = txtRng.Find(TargetList(i)) 

        '~~~> If found 
        Do While Not rngFound Is Nothing 
         '~~> Set the marker so that the next find starts from here 
         n = rngFound.Start + 1 
         '~~> Chnage attributes 
         With rngFound.Font 
          .Bold = msoTrue 
          .Underline = msoTrue 
          .Italic = msoTrue 
          '~~> Find Next instance 
          Set rngFound = txtRng.Find(TargetList(i), n) 
         End With 
        Loop 
       Next 
      End If 
     Next 
    Next 
End Sub 

최종 스크린 샷

enter image description here

+0

내가 생각해내는 것에 매우 가깝게 보입니다. 그래서 나는 올바른 방향으로 가고 있다고 생각합니다. 도와 주셔서 감사합니다! –

+0

와우, 나는 당신에게 또 다른 +1을 줄 대표가 있었으면 좋겠다. 재미 있고 거룩한 코드로 코드를 컴파일 했으므로 내 것보다 10 배는 빠릅니다. 나는 당신의 for 루프가 중첩 된 모든 텍스트 박스에서 각 단어를 찾는 반복문과 한 단어에 대한 전체 프리젠 테이션을 검색하는 루프를 반복 한 다음 다음 단어에 대한 전체 프리젠 테이션을 다시 검색하는 것의 차이점이라고 생각합니다. 다시 한 번 감사드립니다. 귀하의 사례를 통해 효율성에 대해 많은 것을 배웠습니다. -Ryan –

+0

기본적으로 필자가 사용한 방법 (PowerPoint 2013의 경우 어쨌든)은 Find() 함수가 일치하는 항목이 없을 때 Nothing을 반환하지 않고 빈 TextRange 개체를 대신 반환 할 수 있다는 것 . 이것은 PowerPoint 버그처럼 보입니다. 따라서 내 회피 코드는 Do not Not rngFound IsNothing 및 Alng rngFound.Length> 0과 동일합니다. – OfficeAddinDev

1

내가 좋은 오히려 (1 awarder 나에게) 추천 @Siddharth 패주 응답을 확장하고 싶습니다. 그러나 PP에서 단어 (단어의 범위)를 '강조 표시'할 수도 있습니다. 하이라이트를 설정하는 데 심각한 단점이 있습니다. 다른 글꼴 설정을 파괴합니다. 따라서 실제로 하이라이트를 사용해야하는 경우 나중에 적절한 글꼴 설정을 반환해야합니다. 용액의 종류가 @Siddharth 용액 안에 어딘가에 배치 될 수

Sub Highlight_Word() 

Dim startSize, startFont, startColor 

With ActivePresentation.Slides(1).Shapes(1).TextFrame2.TextRange.Words(8).Font 
'read current state 
    startSize = .Size 
    startFont = .Name 
    startColor = .Fill.ForeColor.RGB 

'set highlight 
    .Highlight.RGB = RGB(223, 223, 223) 'light grey 

'return standard parameters 
    .Size = startSize 
    .Name = startFont 
    .Fill.ForeColor.RGB = startColor 

End With 

End Sub 

: 여기

단일 텍스트 프레임에서 하나의 단어에 대한 예이다.

+0

기술적으로 가능한 강조 표시가 유용합니다. 의견을 보내 주셔서 감사합니다. –

+0

".Highlight.RGB ="줄이 나에게이 오류를 보냈습니다 : 컴파일 오류 : 메서드 또는 데이터 멤버를 찾을 수 없음 –

+0

사용하려면 PPT 2010 (또는 2007) 이상을 실행해야합니다. 하이라이트 –

0

그리고 당신은 완전히 원래의 텍스트 서식을 보존해야하는 경우, 당신은 할 수 :

대상 텍스트를 포함하는 형태를 찾는

, 는 원래의 모양의 Z 순서 할 수있는 중복 보내기 모양 중복 중복 된 모양에 대한 강조 표시 복제본과 원본 모두에 태그를 적용하여 나중에주의해야 함을 나타냅니다. 예 는 oOriginalShape.Tags.Add "Hilighting는", "원래" 는 강조 표시를 반전하고 원본을 복원해야하는 경우 "Hilighting는"그런 설정 보이지 않는 원래의 모양

을 "중복"oDupeShape.Tags.Add 형식을 지정하면 모든 모양을 반복 할 수 있습니다. 도형에 Hilighting tag = "Original"이 있으면 그것을 보이게하십시오. Higlighting tag = "Duplicate"이면 삭제하십시오.

누군가가 강조 표시된 모양을 편집하면 되돌릴 때 편집 내용이 손실됩니다. 사용자는 되돌리기, 편집, 강조 표시를해야합니다.