ttt480 Posted December 1, 2018 Share Posted December 1, 2018 I have a Word document. This document has text written in green. The document also contains text highlighted in yellow. How to use the autoit script to determine the number of green characters and the number of yellow backlight characters ? 1.doc Link to comment Share on other sites More sharing options...
water Posted December 1, 2018 Share Posted December 1, 2018 You can use _Word_DocFind to search for formatting as well. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Skysnake Posted December 4, 2018 Share Posted December 4, 2018 Hey @water, I was also looking at something like this. Question is, all the Object commands, where are they specified eg $oRange.Bold <== Some are obvious, but where can I get a list? Like in this post, how do I set the Character foreground, background color? Skysnake Skysnake Why is the snake in the sky? Link to comment Share on other sites More sharing options...
water Posted December 4, 2018 Share Posted December 4, 2018 MSDN is your friend https://docs.microsoft.com/en-us/office/vba/api/word.range.bold My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Aether Posted December 4, 2018 Share Posted December 4, 2018 I am trying to locate some specific formats with no success... #include <Constants.au3> #include <GUIConstants.au3> #include <Word.au3> Opt ("MustDeclareVars", 1) Global $oWord = _Word_Create() $oWord.activate Local $oDoc = _Word_DocOpen($oWord, "V.doc", Default, Default, True) $oWord.ActiveDocument.Content.Find.Font.Bold = True Local $oRangeFound = _Word_DocFind($oDoc, "", 0, Default, Default, Default, Default, Default, Default, Default, True) If @error Then MsgBox($MB_SYSTEMMODAL, "", "@error = " & @error & ", @extended = " & @extended) Trying to find bold characters. Getting error = 4. Link to comment Share on other sites More sharing options...
water Posted December 4, 2018 Share Posted December 4, 2018 I always wondered how the function would find formatted text (grabbed the example from a VB book and translated to AutoIt). Now I know: It doesn't work. The following example works. It searches for the first bold text and underlines the found word. I had to create a custom find function and added a single line (marked <==): expandcollapse popup#include <MsgBoxConstants.au3> #include <Word.au3> Global Const $wdUnderlineSingle = 1 ; Create application object Local $oWord = _Word_Create() If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open test document read-only Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test2.doc", Default, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error opening 'Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Find the first bold word and underline it Local $oRangeFound #forceref $oRangeFound $oRangeFound = _Word_DocFindEX($oDoc, "", 0, Default, False, Default, Default, Default, Default, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error locating the specified text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound.Font.underline = $wdUnderlineSingle MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "first bold text underlined.") Func _Word_DocFindEX($oDoc, $sFindText = Default, $vSearchRange = Default, $oFindRange = Default, $bForward = Default, $bMatchCase = Default, $bMatchWholeWord = Default, $bMatchWildcards = Default, $bMatchSoundsLike = Default, $bMatchAllWordForms = Default, $bFormat = Default) ; Error handler, automatic cleanup at end of function Local $oError = ObjEvent("AutoIt.Error", "__Word_COMErrFunc") #forceref $oError If $sFindText = Default Then $sFindText = "" If $vSearchRange = Default Then $vSearchRange = 0 If $bForward = Default Then $bForward = True If $bMatchCase = Default Then $bMatchCase = False If $bMatchWholeWord = Default Then $bMatchWholeWord = False If $bMatchWildcards = Default Then $bMatchWildcards = False If $bMatchSoundsLike = Default Then $bMatchSoundsLike = False If $bMatchAllWordForms = Default Then $bMatchAllWordForms = False If $bFormat = Default Then $bFormat = False If Not IsObj($oDoc) Then Return SetError(1, 0, 0) Switch $vSearchRange Case -1 $vSearchRange = $oDoc.Application.Selection.Range Case 0 $vSearchRange = $oDoc.Range() Case Else If Not IsObj($vSearchRange) Then Return SetError(2, 0, 0) EndSwitch If $oFindRange = Default Then $oFindRange = $vSearchRange.Duplicate() Else If Not IsObj($oFindRange) Then Return SetError(3, 0, 0) If $bForward = True Then $oFindRange.Start = $oFindRange.End ; Search forward $oFindRange.End = $vSearchRange.End Else $oFindRange.End = $oFindRange.Start ; Search backward $oFindRange.Start = $vSearchRange.Start EndIf EndIf $oFindRange.Find.ClearFormatting() $oFindRange.Find.Font.Bold = True ; <== had to add this line!! $oFindRange.Find.Execute($sFindText, $bMatchCase, $bMatchWholeWord, $bMatchWildcards, $bMatchSoundsLike, _ $bMatchAllWordForms, $bForward, $WdFindStop, $bFormat) If @error Or Not $oFindRange.Find.Found Then Return SetError(4, @error, 0) Return $oFindRange EndFunc ;==>_Word_DocFind My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Aether Posted December 4, 2018 Share Posted December 4, 2018 Thanks I was wondering what I was doing wrong Link to comment Share on other sites More sharing options...
Aether Posted December 4, 2018 Share Posted December 4, 2018 (edited) 1 hour ago, water said: I had to create a custom find function and added a single line Change a bit the function, so now we can define search criteria outside the func expandcollapse popup#include <Constants.au3> #include <GUIConstants.au3> #include <Word.au3> Global Const $wdUnderlineSingle = 1 Global Const $wdYellow = 7 Local $oWord = _Word_Create() $oWord.activate Local $oDoc = _Word_DocOpen($oWord, "v.doc", Default, Default, True) Local $oRange = $oDoc.Content $oRange.Find.Font.Bold = True $oRange.Find.Highlight = $wdYellow Local $oRangeFound #forceref $oRangeFound $oRangeFound = _Word_DocFindEX($oRange, "", True, Default, Default, Default, Default, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error locating the specified text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound.Font.underline = $wdUnderlineSingle MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "first bold text underlined.") Func _Word_DocFindEX($oRange, $sFindText = "", $bForward = Default, $bMatchCase = Default, _ $bMatchWholeWord = Default, $bMatchWildcards = Default, $bMatchSoundsLike = Default, $bMatchAllWordForms = Default, $bFormat = Default) ; Error handler, automatic cleanup at end of function Local $oError = ObjEvent("AutoIt.Error", "__Word_COMErrFunc") #forceref $oError If $bForward = Default Then $bForward = True If $bMatchCase = Default Then $bMatchCase = False If $bMatchWholeWord = Default Then $bMatchWholeWord = False If $bMatchWildcards = Default Then $bMatchWildcards = False If $bMatchSoundsLike = Default Then $bMatchSoundsLike = False If $bMatchAllWordForms = Default Then $bMatchAllWordForms = False If $bFormat = Default Then $bFormat = False If Not IsObj($oRange) Then Return SetError(2, 0, 0) $oRange.ClearFormatting () $oRange.Find.Execute($sFindText, $bMatchCase, $bMatchWholeWord, $bMatchWildcards, $bMatchSoundsLike, _ $bMatchAllWordForms, $bForward, $WdFindStop, $bFormat) If @error Or Not $oRange.Find.Found Then Return SetError(4, @error, 0) Return $oRange EndFunc ;==>_Word_DocFindEX And it is working fine forward and backward ! nn to switch start and end Edited December 4, 2018 by Aether Link to comment Share on other sites More sharing options...
water Posted December 4, 2018 Share Posted December 4, 2018 Thanks! Will have a look at it as soon as I find some spare time My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
ttt480 Posted December 7, 2018 Author Share Posted December 7, 2018 Aether - The script does not work. Constantly reports " Error locating the specified text in the document." Link to comment Share on other sites More sharing options...
Aether Posted December 7, 2018 Share Posted December 7, 2018 8 minutes ago, ttt480 said: Aether - The script does not work. Constantly reports " Error locating the specified text in the document." Well it is working fine for me. Post your code, so we shall see Link to comment Share on other sites More sharing options...
ttt480 Posted December 7, 2018 Author Share Posted December 7, 2018 Aether - I'm using your code. The script simply opens file 1.doc and displays a message " Error locating the specified text in the document." I have MSWord 2003. expandcollapse popup#include <Constants.au3> #include <GUIConstants.au3> #include <Word.au3> Global Const $wdUnderlineSingle = 1 Global Const $wdYellow = 7 Local $oWord = _Word_Create() $oWord.activate Local $oDoc = _Word_DocOpen($oWord, "1.doc", Default, Default, True) Local $oRange = $oDoc.Content $oRange.Find.Font.Bold = True $oRange.Find.Highlight = $wdYellow Local $oRangeFound #forceref $oRangeFound $oRangeFound = _Word_DocFindEX($oRange, "", True, Default, Default, Default, Default, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "Error locating the specified text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) $oRangeFound.Font.underline = $wdUnderlineSingle MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _ "first bold text underlined.") Func _Word_DocFindEX($oRange, $sFindText = "", $bForward = Default, $bMatchCase = Default, _ $bMatchWholeWord = Default, $bMatchWildcards = Default, $bMatchSoundsLike = Default, $bMatchAllWordForms = Default, $bFormat = Default) ; Error handler, automatic cleanup at end of function Local $oError = ObjEvent("AutoIt.Error", "__Word_COMErrFunc") #forceref $oError If $bForward = Default Then $bForward = True If $bMatchCase = Default Then $bMatchCase = False If $bMatchWholeWord = Default Then $bMatchWholeWord = False If $bMatchWildcards = Default Then $bMatchWildcards = False If $bMatchSoundsLike = Default Then $bMatchSoundsLike = False If $bMatchAllWordForms = Default Then $bMatchAllWordForms = False If $bFormat = Default Then $bFormat = False If Not IsObj($oRange) Then Return SetError(2, 0, 0) $oRange.ClearFormatting () $oRange.Find.Execute($sFindText, $bMatchCase, $bMatchWholeWord, $bMatchWildcards, $bMatchSoundsLike, _ $bMatchAllWordForms, $bForward, $WdFindStop, $bFormat) If @error Or Not $oRange.Find.Found Then Return SetError(4, @error, 0) Return $oRange EndFunc ;==>_Word_DocFindEX Link to comment Share on other sites More sharing options...
Aether Posted December 7, 2018 Share Posted December 7, 2018 (edited) $oRange.Find.Font.Bold = True $oRange.Find.Highlight = $wdYellow Those are not OR they are AND. So part of your text in the doc must be bolded with a yellow background. Otherwise it will return an error. I also have ms-office 2003 Edited December 7, 2018 by Aether Link to comment Share on other sites More sharing options...
ttt480 Posted December 7, 2018 Author Share Posted December 7, 2018 But I didn't ask for bold. I asked about the selection of text in green and yellow. The question was: How to use the autoit script to determine the number of green characters and the number of yellow backlight characters ? Link to comment Share on other sites More sharing options...
Nine Posted December 7, 2018 Share Posted December 7, 2018 Just now, ttt480 said: But I didn't ask for bold. Dude just remove that line : $oRange.Find.Font.Bold = True “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
ttt480 Posted December 7, 2018 Author Share Posted December 7, 2018 Nine, The line was removed. Now the script displays the message "First bold text underlined". Link to comment Share on other sites More sharing options...
Aether Posted December 7, 2018 Share Posted December 7, 2018 sigh... 8 minutes ago, ttt480 said: The line was removed. Now the script displays the message "First bold text underlined". It means it is working. Now that you have the base code, you're gonna have to modify it to fit your needs. Link to comment Share on other sites More sharing options...
ttt480 Posted December 7, 2018 Author Share Posted December 7, 2018 Aether, I assure you, the code doesn't work. It does not count the text of the desired color. Link to comment Share on other sites More sharing options...
Aether Posted December 7, 2018 Share Posted December 7, 2018 (edited) 2 hours ago, ttt480 said: I assure you, the code doesn't work. 3 hours ago, ttt480 said: But I didn't ask for bold You didn't read the rules when you came here. Read them carefully (it's sticky up there). You cannot ASK for anything here. You need to show some effort in scripting your own program. Then we may try to help you. I know my script doesn't count chars or words. But from now on you will have to invest time and effort to program your own script based on your needs. I didn't report you so far, but I guess you won't get that lucky anymore... Edited December 7, 2018 by Aether Link to comment Share on other sites More sharing options...
Subz Posted December 8, 2018 Share Posted December 8, 2018 (edited) Determine the number of green characters and the number of yellow backlight characters ? expandcollapse popup#include <Constants.au3> #include <GUIConstants.au3> #include <Word.au3> Global Const $wdGreen = 4 Global Const $wdYellow = 7 Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\1.doc") Local $nGreen = 0, $nYellow = 0 Local $oRangeFound = Default While 1 $oRangeFound = _Word_DocFindEX($oDoc, "", 0, $oRangeFound, True, Default, Default, Default, Default, Default, True) If @error Then ExitLoop If $oRangeFound.HighlightColorIndex = $wdGreen Then $nGreen += 1 If $oRangeFound.HighlightColorIndex = $wdYellow Then $nYellow += 1 WEnd MsgBox(4096, "Results", "Number of Green Highlighted: " & $nGreen & @CRLF & "Number of Yellow Highlighted: " & $nYellow) Func _Word_DocFindEx($oDoc, $sFindText = Default, $vSearchRange = Default, $oFindRange = Default, $bForward = Default, $bMatchCase = Default, $bMatchWholeWord = Default, $bMatchWildcards = Default, $bMatchSoundsLike = Default, $bMatchAllWordForms = Default, $bFormat = Default) ; Error handler, automatic cleanup at end of function Local $oError = ObjEvent("AutoIt.Error", "__Word_COMErrFunc") #forceref $oError If $sFindText = Default Then $sFindText = "" If $vSearchRange = Default Then $vSearchRange = 0 If $bForward = Default Then $bForward = True If $bMatchCase = Default Then $bMatchCase = False If $bMatchWholeWord = Default Then $bMatchWholeWord = False If $bMatchWildcards = Default Then $bMatchWildcards = False If $bMatchSoundsLike = Default Then $bMatchSoundsLike = False If $bMatchAllWordForms = Default Then $bMatchAllWordForms = False If $bFormat = Default Then $bFormat = False If Not IsObj($oDoc) Then Return SetError(1, 0, 0) Switch $vSearchRange Case -1 $vSearchRange = $oDoc.Application.Selection.Range Case 0 $vSearchRange = $oDoc.Range() Case Else If Not IsObj($vSearchRange) Then Return SetError(2, 0, 0) EndSwitch If $oFindRange = Default Then $oFindRange = $vSearchRange.Duplicate() Else If Not IsObj($oFindRange) Then Return SetError(3, 0, 0) If $bForward = True Then $oFindRange.Start = $oFindRange.End ; Search forward $oFindRange.End = $vSearchRange.End Else $oFindRange.End = $oFindRange.Start ; Search backward $oFindRange.Start = $vSearchRange.Start EndIf EndIf $oFindRange.Find.ClearFormatting() $oFindRange.Find.Highlight = True $oFindRange.Find.Execute($sFindText, $bMatchCase, $bMatchWholeWord, $bMatchWildcards, $bMatchSoundsLike, _ $bMatchAllWordForms, $bForward, $WdFindStop, $bFormat) If @error Or Not $oFindRange.Find.Found Then Return SetError(4, @error, 0) Return $oFindRange EndFunc ;==>_Word_DocFindEx Edited December 8, 2018 by Subz 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