Jump to content

Word.au3 - search outside the main body of the Word doc


arts
 Share

Go to solution Solved by arts,

Recommended Posts

Hello,
I can search through a Word document using Word.au3 but the search is limited to the main body of the document. Is it possible to search in the other parts like headers, footers, text-boxes, footnotes, image legends/captions or any other area that is outside the main body? I think this is called the StoryRanges of the StoryTypes
as decribed in this macro . Can it be done with AutoIt? Thank you.

Edited by arts
Link to comment
Share on other sites

Parameter $vSearchRange allows to specify the search range. What happens when you pass the needed StoryRange?

Can't test at the moment as I have no Office installed here.

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

Parameter $vSearchRange allows to specify the search range. What happens when you pass the needed StoryRange?

Can't test at the moment as I have no Office installed here.

I'll try and let you know later, Water. Thank you.

Link to comment
Share on other sites

Well, using this modified piece of code from your  "_Word_DocFind Example" I can find the word "Story" in Footnotes. Now I 'only' have to find a way of looping over the other parts of the document too... :sweating:

Local $Footnotes = $oDoc.Footnotes.Count
ConsoleWrite("Footnotes: " & $Footnotes & @CRLF )

   For $n = 1 to $Footnotes
      $MyFootnote = $oDoc.Footnotes($n).range
      $oRangeFound = _Word_DocFind($oDoc, "Story", $MyFootnote, Default, False)
      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.Bold = True
      MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFind Example", _
            "Last occurrence of string 'Story' in the document marked as bold.")
   Next
Edited by arts
Link to comment
Share on other sites

Global Const $wdFootnotesStory = 2
For $oStory In $oDoc.StoryRanges ; Process all Stories of the document
    If $oStory = $wdFootnotesStory Then ; Only process Footnotes
        $oStoryRange = $oStory
        While 1 ; Loop through the footnotes
            $oStoryRange = $oStoryEange.NextStoryRange
            If $oStoryRange = 0 Then ExitLoop ; No more footnotes
            ; Process $oStory.NextStoryRange here
        WEnd
    EndIf
Next

Untested example how to loop through the stories and process all footnotes.

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

Global Const $wdFootnotesStory = 2
For $oStory In $oDoc.StoryRanges ; Process all Stories of the document
    If $oStory = $wdFootnotesStory Then ; Only process Footnotes
        $oStoryRange = $oStory
        While 1 ; Loop through the footnotes
            $oStoryRange = $oStoryEange.NextStoryRange
            If $oStoryRange = 0 Then ExitLoop ; No more footnotes
            ; Process $oStory.NextStoryRange here
        WEnd
    EndIf
Next

Untested example how to loop through the stories and process all footnotes.

I couldn't make it work, water. Probably, my fault. Note that my solution above works and gets all Footnotes in the document (tested with a 15 pages doc with many footnotes in each page). But I need more than footnotes. So, now I need to create a global routine that can address headers, footers, text-boxes, footnotes, image legends/caption, comments, etc. in a run. Your approach with '$oDoc.StoryRanges' seems very interesting and I'll try to explore it tomorrow. Thank you!

Edited by arts
Link to comment
Share on other sites

The following script loops through all Stories, processes the footnotes and writes them to the Console.

#include <word.au3>
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx")
Global Const $wdFootnotesStory = 2
For $oStory In $oDoc.StoryRanges ; Process all Stories of the document
    If $oStory.StoryType = $wdFootnotesStory Then ; Only process Footnotes
        $oStoryRange = $oStory
        While $oStoryRange.NextStoryRange <> 0 ; Loop through the footnotes
            ; Process $oStory.NextStoryRange here
            ConsoleWrite("Footnotes: " & @CRLF & $oStoryRange.Text)
            $oStoryRange = $oStoryRange.NextStoryRange
        WEnd
    EndIf
Next

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

  • Solution

 

The following script loops through all Stories, processes the footnotes and writes them to the Console.

#include <word.au3>
$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx")
Global Const $wdFootnotesStory = 2
For $oStory In $oDoc.StoryRanges ; Process all Stories of the document
    If $oStory.StoryType = $wdFootnotesStory Then ; Only process Footnotes
        $oStoryRange = $oStory
        While $oStoryRange.NextStoryRange <> 0 ; Loop through the footnotes
            ; Process $oStory.NextStoryRange here
            ConsoleWrite("Footnotes: " & @CRLF & $oStoryRange.Text)
            $oStoryRange = $oStoryRange.NextStoryRange
        WEnd
    EndIf
Next

Yes, this one gets every footnote. I'll explore this to try to get all I need. Thank you very much, water! :)

Link to comment
Share on other sites

:)

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