Jump to content
Sign in to follow this  
ttt480

Define letters in a Word document with a specific format

Recommended Posts

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

Share this post


Link to post
Share on other sites

You can use _Word_DocFind to search for formatting as well.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 <==):

#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 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


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

#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 by Aether

Share this post


Link to post
Share on other sites

Thanks! Will have a look at it as soon as I find some spare time :)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


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

Share this post


Link to post
Share on other sites

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.

#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

 

Share this post


Link to post
Share on other sites
$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 :lol:

Edited by Aether

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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 by Aether

Share this post


Link to post
Share on other sites

Determine the number of green characters and the number of yellow backlight characters ?

#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 by Subz

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...