Jump to content

MS Word doc, change text color.


Recommended Posts

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
Link to comment
Share on other sites

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

_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

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...