Find and Highlight Text in MS PowerPoint -
i used code site make macro keyword search on word docs , highlight results.
i replicate effect in powerpoint.
here code word.
sub highlightkeywords() dim range range dim long dim targetlist targetlist = array("keyword", "second", "third", "etc") ' array of terms search = 0 ubound(targetlist) ' length of array set range = activedocument.range range.find ' find text withing range "active document" .text = targetlist(i) ' has words array targetlist .format = true ' same format .matchcase = false ' , case insensitive .matchwholeword = true ' , not part of larger word .matchallwordforms = false ' , not search permutations of word while .execute(forward:=true) range.highlightcolorindex = wdyellow ' highlight keywords loop yellow loop end next end sub here have far in powerpoint, in no way functional.
sub highlightkeywords() dim range range dim long dim targetlist targetlist = array("keyword", "second", "third", "etc") ' array of terms search each sld in application.activepresentation.slides each shp in sld.shapes if shp.hastextframe set txtrng = shp.textframe.textrange = 0 ubound(targetlist) ' length of array range.txtrng ' find text withing range "shape, text frame, text range" .text = targetlist(i) ' has words array targetlist .format = true ' same format .matchcase = false ' , case insensitive .matchwholeword = true ' , not part of larger word .matchallwordforms = false ' , not search permutations of word while .execute(forward:=true) range.highlightcolorindex = wdyellow ' highlight keywords loop yellow loop end next end sub i ended finding answer through msdn, close answer selected correct people submitted.
here code went with:
sub keywords() dim targetlist dim element variant targetlist = array("first", "second", "third", "etc") each element in targetlist each sld in application.activepresentation.slides each shp in sld.shapes if shp.hastextframe set txtrng = shp.textframe.textrange set foundtext = txtrng.find(findwhat:=element, matchcase:=false, wholewords:=true) while not (foundtext nothing) foundtext .font.bold = true .font.color.rgb = rgb(255, 0, 0) end loop end if next next next element end sub turns out code worked, performance nightmare. code selected correct answer below runs more smoothly. i've adjusted program match answer selected.
afaik there no inbuilt way highlight found word color. go out of way create rectangular shape , place behind found text , color different ball game altogether.
here example search text in slides , make found text bold, underline , italicized. if want can change color of font.
let's have slide looks this

paste code in module , try it. have commented code not have problem understanding it.
option explicit sub highlightkeywords() dim sld slide dim shp shape dim txtrng textrange, rngfound textrange dim long, n long dim targetlist '~~> array of terms search targetlist = array("keyword", "second", "third", "etc") '~~> loop through each slide each sld in application.activepresentation.slides '~~> loop through each shape each shp in sld.shapes '~~> check if has text if shp.hastextframe set txtrng = shp.textframe.textrange = 0 ubound(targetlist) '~~> find text set rngfound = txtrng.find(targetlist(i)) '~~~> if found while not rngfound nothing '~~> set marker next find starts here n = rngfound.start + 1 '~~> chnage attributes rngfound.font .bold = msotrue .underline = msotrue .italic = msotrue '~~> find next instance set rngfound = txtrng.find(targetlist(i), n) end loop next end if next next end sub final screenshot

Comments
Post a Comment