Jump to content

Word: Find between ranges


frank10
 Share

Go to solution Solved by water,

Recommended Posts

I have a doc like this:

example 1
    10§ minutes
    do something else
    bla bla bla
    
example 2
    bla bla
    other bla bla
    
example 3
    45§ minutes
    bla bla
    other 12§ minutes, then bla bla

I search for every example and then I'll search for "§ minutes", but I want to change it only if it's related to the current example.

like this:

global $oSearchRange = _Word_DocRangeSet($oDoc, -1)
global  $oRangeFound 
While 1
    $oRangeFound = _Word_DocFind($oDoc, "example", $oSearchRange)
    local $err = @error, $ext = @extended
    if $debug = 1 then Consolewrite( "example: __err:" & $err & "__extended:" & $ext & @crlf)
    if $err then Exitloop
    
    $oRangeFound = _Word_DocFind($oDoc, "§ minutes", $oSearchRange)
    $oRangeFound = _Word_DocRangeSet($oDoc, $oRangeFound, $wdWord, -2, $wdParagraph, 1) 
    msgbox(0,'§ minutes', $oRangeFound.Text)

Wend


i.e.

example 1 will find its row 10§ minutes,

BUT when I go to example 2, the search for minutes will fall into a row of example 3 because there is no minutes on example 2...

How can I detect if a Find is within the current example?

Link to comment
Share on other sites

  • Solution

I would use two loops to find 1) all occurrences of "Example" and 2) all occurrences of "§minutes".
_Word_DocFind returns a range. With

Global Const $wdFirstCharacterLineNumber = 10 ; https://docs.microsoft.com/en-us/office/vba/api/Word.WdInformation
$iLineNumber = $oRangeFound.Information($wdFirstCharacterLineNumber)

you get the line number.
Put all line numbers (plus a flag that tells you if the entry is an "example" or a "§minutes" line) into an array, sort them and voila!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

After more digging, it's not perfect: it returns only line numbers relative to page... so it must be combined with page Number too.
It would be perfect if there exists an absolute line number...

Or I could extend the range from 1° example to 2° example and comparing characters' position of the range with .Start and .End.

Link to comment
Share on other sites

To retrieve the page number you could use

Global Const $wdFirstCharacterLineNumber = 10 ; https://docs.microsoft.com/en-us/office/vba/api/Word.WdInformation
Global Const $wdActiveEndPageNumber = 3 
$iPageNumber = $oRangeFound.Information($wdActiveEndPageNumber)
$iLineNumber = $oRangeFound.Information($wdFirstCharacterLineNumber)
$iUniqueNumber = ($iPageNumber * 1000) + $iLineNumber

So you get a unique number for each hit in the whole document.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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...