Jump to content

_Word_DocFind find all occurencies


Recommended Posts

Hi, I have this code:

#include <array.au3>
#include <word.au3>

Global $arrHeader = ["AAA", "BBB", "CCC"]

$apl = _Word_Create()
$objwBook = _Word_DocAttach($apl, "C:\Users\ME\Desktop\script\Doc.docx")

For $i = 0 To UBound($arrHeader) - 1

    Global $objText = _Word_DocFind($objwBook, $arrHeader[$i])

    If $objText <> 0 Then $objText.Bold = True

Next

And the in the Word doc I have this:

AAA, BBB, CCC

AAA, BBB, CCC

Current code only puts Bold to the first AAA, BBB, CCC.

Second line in not touched.

_Word_DocFind($objwBook, $arrHeader[$i]) finds first AAA, puts Bold on it and goes to find BBB then.

I can't figure out how to make it go through the whole documents until it finds and Bolds all AAA and only then goes to BBB.

Please help.

Edited by stick3r
Link to comment
Share on other sites

HAHA, you should feel lucky, I don't often do this. this works with your test doc and is taken from example in help and for loop from your code above

#include <MsgBoxConstants.au3>
#include <Word.au3>

Global $arrHeader = ["AAA", "BBB", "CCC"]

; 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 & "\Test.docx", Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _
        "Error opening 'Test.docx'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Underline All groups of text in the array.
Local $oRangeFound, $oSearchRange
$oSearchRange = _Word_DocRangeSet($oDoc, 1)

For $i = 0 To UBound($arrHeader) - 1
  $oRangeFound = _Word_DocFind($oDoc, $arrHeader[$i], $oSearchRange)
    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.Underline = True

    While 1
        $oRangeFound = _Word_DocFind($oDoc, $arrHeader[$i], $oSearchRange, $oRangeFound)
        If @error Then ExitLoop
        $oRangeFound.Underline = True
    WEnd

Next

I included a .docx and a .doc (just change the code above to reference the older type .doc if that is what you need, both work.

Quote

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

AAA, BBB, CCC

 

Test.docx

 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

When _Word_DocFind is called for the first time without specifying the $oFindRange parameter it returns the first occurrence. Then you need to call _Word_DocFind again and pass $oFindRange. So the function knows where to continue searching for the second occurrence. Repeat this until no further occurrences get found.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Do we just talk about the main story or about headers, footers, Endnotes, Footnotes, Text or Textframes as well?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Please have a look at this thread:

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...