How to make a VBA word code run faster? -
i found code online search , highlight multiple words. takes 10 min run on 15 page document. wondering if made run faster.
sub highlightmultiplewords() dim word range dim wordcollection(2) string dim words variant 'define list. 'if add or delete, change value above in dim statement. wordcollection(0) = "word1" wordcollection(1) = "word2" wordcollection(2) = "word3" 'set highlight color. options.defaulthighlightcolorindex = wdyellow 'clear existing formatting , settings in find feature. selection.find.clearformatting selection.find.replacement.clearformatting 'set highlight replace setting. selection.find.replacement.highlight = true 'cycle through document , find words in collection. 'highlight words when found. each word in activedocument.words each words in wordcollection selection.find .text = words .replacement.text = "" .forward = true .wrap = wdfindcontinue .format = true .matchcase = false .matchwholeword = false .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end selection.find.execute replace:=wdreplaceall next next end sub
the comments correct here, need run find , replace once per item in list, running multiple times amount of words in document.
option explicit sub highlightmultiplewords() dim arywords(2) string dim vntstore variant 'define list. 'if add or delete, change value above in dim statement. arywords(0) = "word1" arywords(1) = "word2" arywords(2) = "word3" 'set highlight color. options.defaulthighlightcolorindex = wdyellow selection.find 'clear existing formatting , settings in find feature. .clearformatting .replacement.clearformatting 'set highlight replace setting. selection.find.replacement.highlight = true 'process array each vntstore in arywords .execute findtext:=vntstore, _ matchcase:=false, _ matchwholeword:=false, _ matchwildcards:=false, _ matchsoundslike:=false, _ matchallwordforms:=false, _ forward:=true, _ wrap:=wdfindcontinue, _ format:=true, _ replace:=wdreplaceall next end end sub
Comments
Post a Comment