Jump to content
Sign in to follow this  
sirkerry

Search & Replace in Word, plain text to bold text

Recommended Posts

sirkerry

I've got _WordDocFindReplace working fine for searching and replacing text in a Word .doc, but how do I get it to replace plain text with bolded text?

Edited by sirkerry

Share this post


Link to post
Share on other sites
Spiff59

This'll do it...

#include <Excel.au3>

$oExcel = _ExcelBookOpen(@ScriptDir & "\test.xls", 0, 0)
$oExcel.Activesheet.Range("C10").Font.Bold = True
_ExcelBookClose($oExcel,1)

Edit: Poop! Shame on me... you said Word, not Excel.

$oWord = ObjCreate("Word.Application")
$oDoc = $oWord.Documents.Open(@ScriptDir & "\test.doc")
$oDoc.Content.Words(4).Bold = True
$oDoc.Save
$oWord.Application.Quit
$oDoc = 0
$oWord = 0
Edited by Spiff59

Share this post


Link to post
Share on other sites
sirkerry

Well that sort of works, it bolds the 4th word in the document, but what I want to do is bold all of the text that matches a string, say "This Needs To Be Bold".

This'll do it...

Edit: Poop! Shame on me... you said Word, not Excel.

$oWord = ObjCreate("Word.Application")
$oDoc = $oWord.Documents.Open(@ScriptDir & "\test.doc")
$oDoc.Content.Words(4).Bold = True
$oDoc.Save
$oWord.Application.Quit
$oDoc = 0
$oWord = 0

Edited by sirkerry

Share this post


Link to post
Share on other sites
Spiff59

You''ll have to tweak things the way you want them. This has a search built in it.

MSDN is a great resource for this stuff.

_Word_Bold_Text(@ScriptDir & "\test.doc", "This text should be bold")

Func _Word_Bold_Text($doc, $str)
    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
    EndIf
    $oRange = 0
    If Not IsObj($doc) Then
        $oDoc.Save
        $oWord.Application.Quit
        $oWord = 0
    EndIf
    $oDoc = 0
EndFunc

Edit: added the $doc parm

Edit2: Allow the $doc parameter to be either an object to an already open workbook, or, a full pathname to an unopened document.

Edited by Spiff59

Share this post


Link to post
Share on other sites
sirkerry

You''ll have to tweak things the way you want them. This has a search built in it.

MSDN is a great resource for this stuff.

Thanks, that put me on the right track.

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  

×