Sign in to follow this  
Followers 0
arts

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

9 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
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
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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

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

Share this post


Link to post
Share on other sites
:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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  
Followers 0