sirkerry Posted August 12, 2010 Posted August 12, 2010 (edited) 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 August 12, 2010 by sirkerry
Spiff59 Posted August 12, 2010 Posted August 12, 2010 (edited) 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 August 13, 2010 by Spiff59
sirkerry Posted August 13, 2010 Author Posted August 13, 2010 (edited) 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 August 13, 2010 by sirkerry
Spiff59 Posted August 13, 2010 Posted August 13, 2010 (edited) 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 August 17, 2010 by Spiff59
sirkerry Posted August 17, 2010 Author Posted August 17, 2010 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.
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