Jump to content
Sign in to follow this  

Microsoft Word - read data and export to a text file

Recommended Posts

Is it possible to read data in a microsoft word( .doc ) file searching for specific criteria and then copy a line in the file where the data is found...export to a text file. I have been playing around with the word.au3 and there is a replace string but no find option. Has there been an update to the word.au3 or can anyone else give me a way of doing it, thanks

Share this post

Link to post
Share on other sites

I would like to search for a string in a word document and once the string is found I would like to copy data that is a tab over from the search string. I do not see a Find function in the word.au3/udf, there is a FindandReplace. If anyone has done anything like this help would be greatly appreciated, thanks

Share this post

Link to post
Share on other sites

This was a fun one! I learned a lot of new tricks to can be used with ranges.

This may not be exactly what you need, but you should be able to modify it to fit your needs. If you have any more questions feel free to ask.

#include <Word.au3>

$iFound = 0
$oWordApp = _WordCreate (@ScriptDir & "\Test.doc")
$oDoc = _WordDocGetCollection ($oWordApp, 0)
; Specifies a range object of the entire document
$oRng = $oDoc.Range

; Loop through found items
While Not @error
    ; Search for "This" in the range specified above
    ; After searching the range is changed to the found text
    _WordDocFindReplace ($oDoc, "This", "", 0, $oRng, 0, 0, 0, 0, 0, 1, 0)
    If Not @error Then
        ; Duplicates range leaving the original unchanged
        $oTempRng = $oRng.Duplicate
        ; Expands the new range to the start and end of the current sentence
        $oTempRng.SetRange ($oTempRng.Sentences (1).Start, $oTempRng.Sentences (1).End)
        ; Return all text within the range to the console
        ConsoleWrite($oTempRng.Text & @CR)
        $iFound += 1

If $iFound > 0 Then
    MsgBox(0, "Success", "Found " & $iFound & " occurrences.")
    MsgBox(0, "Error", "Not Found")
_WordQuit ($oWordApp, 0)

Share this post

Link to post
Share on other sites

For anyone else who comes across this thread during a search and wants to convert an entire Word document to a plain text file (which could then be searched using native AutoIt functions, etc.), Antiword is one possible approach.

This is extremely handy for systems that don't actually have Microsoft Word installed.

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...