ogloed Posted August 8, 2012 Share Posted August 8, 2012 Hello. Is there a way to find text in MS Word doc file and change its color? I need to highlight found strings. Thank you. Link to comment Share on other sites More sharing options...
Spiff59 Posted August 8, 2012 Share Posted August 8, 2012 (edited) You can play with this... _Word_Color_Text(@ScriptDir & "test.doc", "This text should be blue", 0xFF0000) ; Find text and change it's color Func _Word_Color_Text($doc, $str, $color) ; document path or object, text to find, color If IsObj($doc) Then $oDoc = $doc Else $oWord = ObjCreate("Word.Application") $oDoc = $oWord.Documents.Open($doc) EndIf $oRange = $oDoc.Content ; select entire document $oRange.Find.ClearFormatting() ; clear previous search If $oRange.Find.Execute($str) = True Then ; $oRange.Bold = True $oRange.Font.Color = $color EndIf $oRange = 0 If Not IsObj($doc) Then $oDoc.Save $oWord.Application.Quit $oWord = 0 EndIf $oDoc = 0 EndFunc Edit: It's a quick mod of a function to make text bold. It accepts either an object to an already open document (in which case it leaves the document open), or a string filename (in which case it starts Word, opens the document, makes the changes, saves the document, and kills Word). Edited August 8, 2012 by Spiff59 ogloed 1 Link to comment Share on other sites More sharing options...
ogloed Posted August 8, 2012 Author Share Posted August 8, 2012 Spiff59, thank you! It works! Link to comment Share on other sites More sharing options...
ogloed Posted August 8, 2012 Author Share Posted August 8, 2012 One more thing: how to find every string that matches? And does this method work with regular expressions? For instance, I need to find string "123" and this may be in string "12345", and I need to highlight the whole string that contains numbers ("12345"). Link to comment Share on other sites More sharing options...
ogloed Posted August 8, 2012 Author Share Posted August 8, 2012 OK, found a way to find every matching string.While $oRange.Find.Execute($str) = True<expression>WEnd Link to comment Share on other sites More sharing options...
iamtheky Posted August 9, 2012 Share Posted August 9, 2012 (edited) _Word_Color_Text(@ScriptDir & "test.doc", "123", 0xFF0000) ; Find text and change it's color Func _Word_Color_Text($doc, $str, $color) ; document path or object, text to find, color $oWord = ObjCreate("Word.Application") $oWord.Documents.Open($doc) $oWord.selection.find.ClearFormatting() ; clear previous search while $oWord.selection.find.execute($str) = True $oWord.Selection.Expand $oWord.selection.Font.Color = $color $oWord.selection.Find.Forward = True $oWord.Selection.Find.Execute wend $oWord.documents.Save $oWord.Application.Quit $oWord = 0 EndFunc It seems this has some limitations: If you have a line that ends with a standalone 123 then the next line, if beginning with a string that contains 123, that string will not be colored. Also it stops at punctuation so web addresses will not be completely colored. If anybody knows how to remedy those situations, then the above script should do what is requested. Edited August 10, 2012 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now