Jump to content
Sign in to follow this  
ogloed

MS Word doc, change text color.

Recommended Posts

ogloed

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.

Share this post


Link to post
Share on other sites
Spiff59

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 by Spiff59
  • Like 1

Share this post


Link to post
Share on other sites
ogloed

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").

Share this post


Link to post
Share on other sites
ogloed

OK, found a way to find every matching string.

While $oRange.Find.Execute($str) = True

<expression>

WEnd

Share this post


Link to post
Share on other sites
iamtheky

_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 by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×