Jump to content

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


Go to solution Solved by arts,

Recommended Posts

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
Posted

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:

  Reveal hidden contents

 

Posted
  On 11/1/2014 at 11:29 AM, water said:

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.

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
Posted

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:

  Reveal hidden contents

 

Posted (edited)
  On 11/3/2014 at 9:49 PM, water said:
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
Posted

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:

  Reveal hidden contents

 

  • Solution
Posted
  On 11/4/2014 at 8:28 AM, water said:

 

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

Posted
:)

My UDFs and Tutorials:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...