Jump to content

Finding a string in a Word Document


Recommended Posts

I'm using the Word UDF for the first time, and I'm having some trouble with _Word_DocFind(). There isn't really much talk around the forums about this so it's hard to find any support on the issue I'm having. Here's my code:

#include <Word.au3>

$listPath = @ScriptDir & "\AMCH OFFSET 042617.docx"

$pWord = _Word_Create()
$oWord = _Word_DocOpen($pWord, $listPath)

Local $ctr = 0
Local $searchRange = _Word_DocFind($oWord, "Claim Number")
If Not @error Then
    $ctr += 1
EndIf
While ($searchRange <> 0)
    $searchRange = _Word_DocFind($oWord, "Claim Number", 0, $searchRange)
    If Not @error Then
        $ctr += 1
    EndIf
    $searchRange.Select
WEnd

My problem is that it doesn't seem to find a match of the string on any page after the second. When I run the script, it just loops indefinitely on the second page. I can't post an example of the word document because it is medical data, but every page is basically the same and every page has the string I am looking for on it. Also I tried checking @error after doing a find and it is never set, so I don't think that's the problem.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

The following works for me:

#include <Word.au3>
$sListPath = @ScriptDir & "\AMCH OFFSET 042617.docx"

$oWord = _Word_Create()
$oDoc = _Word_DocOpen($oWord, $sListPath)

Local $vSearchRange = 0, $iSearchRange = 0
While 1
    $vSearchRange = $vSearchRange = 0 ? _Word_DocFind($oDoc, "Claim Number") : _Word_DocFind($oDoc, "Claim Number", 0, $vSearchRange)
    If @error Then ExitLoop
    $iSearchRange += 1
    $vSearchRange.Select
WEnd

 

Link to comment
Share on other sites

You do not process the first hit after the first _Word_DocFind. You need something like this (my example file contains: Claim Number x")

#include <Word.au3>

Global $iCounter = 0
Global $sFilePath = @ScriptDir & "\Test.docx"
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, $sFilePath)
Global $oRangeFound = _Word_DocFind($oDoc, "Claim Number")
If Not @error Then
    While 1
        $oRangeLine = _Word_DocRangeSet($oDoc, $oRangeFound, Default, Default, $wdCharacter, 2) ; Extend the range 2 characters to the right 
        ConsoleWrite($oRangeLine.Text & @CRLF) ; Write the found text to the console
        $iCounter += 1
        $oRangeFound = _Word_DocFind($oDoc, "Claim Number", 0, $oRangeFound)
        If @error Then ExitLoop
    WEnd
EndIf
ConsoleWrite($iCounter & " matches found!" & @CRLF)

 

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

Hey guys,

Thanks for the suggestions, but I still have the same issue. I tried both examples and they still don't progress past the second page of the document. I wasn't sure if it was something weird with the document but doing a CTRL + F in the document finds all occurrences of the string. I'll try creating a new document again to see if it works better.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

Can you please post your test document so we can play with it?

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

  • 2 weeks later...

I search all occurrence of word "History" in an ebook. I cant post ebook coz its copyrighted. the loop keeps finding the first word and I pass back original range object. The DocFind correctly calculates Start of next search but the new DocFind iteration keeps finding the first word only, Not sure if something changed in Office 2016 or Win10?

1094-1101
TEST1101 1199965ForwardTrue
1094-1101
TEST1101 1199965ForwardTrue
1094-1101
 

 

Link to comment
Share on other sites

A simple reproducer document would be fine to play with.
There is another thread with the sam problm but th document is very complex.
I'm still running Office 2010 but in the near future will be able to test with Office 2016.

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

Me and water have discussed what the problem possibly is, but he hasn't had time yet to take a look into it. It may be due to how the page breaks are laid out. For the record I'm using Office 2007, which means the issue happens on 2007, 2010, and 2016.

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

Right now I'm sitting in front of my Windows PC and try to find out what causes the problem.
I tested:

  • Section break (next page) - works as expected (finds all occurrences)
  • Section break (continuous)  - works as expected (finds all occurrences)
  • Column break  - works as expected (finds all occurrences)
  • To be continued

 

Edited by water

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

I removed all formatting by copying all content to an empty document and now it works as expected. The function finds all occurrences.
So it is not the function but the formatting that causes the problem.

I have now been playing with the document for more than an hour - to no avail.
In my opinion this is the "strangest" document I have ever layed my eyes upon.

Any change to clean up the formatting of the document? Means it doesn't use table borders but overlays the table with a picture containing a grid etc. etc.

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

To be honest the formatting of the document doesn't matter to me at all. The original document is a PDF file that I actually convert using Acrobat DC to a docx, since I couldn't find another easy way to do so. All I really need to do is grab some information such as the claim number from that document, is that still possible if the formatting is removed?

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

You could write the text content of the document to a flat text file and then process this file:

#include <Word.au3>
#include <MsgBoxConstants.au3>
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\TestSearchFullCompatibilityMode.docx")
Global $sText = $oDoc.Content.Text
FileWrite("C:\temp\tt.txt", $sText)

 

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

That's what I originally tried to do, but it ends up writing things out in weird locations which makes it hard to structure my find correctly. I figured if the document had some structure such as a word document it would be easier to keep track of things because it is separated by pages.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

2 minutes ago, SkysLastChance said:

I can get it in to excel for you if that is any help

Nah, I'm not really trying to get it into Excel, all I need to do is grab some specific information from certain spots.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

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

×
×
  • Create New...