Jump to content

Word document -search Table


spoo
 Share

Recommended Posts

Hi All,

1. I want to search for a unique number in the word document. lets say 34-3330

2. After finding the number i want to search for a key word "MSN INVOLVED" , upwards starting from the  location of 34-3330 (ie. MSN involved keyword will be above  34-3330 number)

3.Get the first  table occurrence with  "MSN INVOLVED " keyword  

>    check if the MSN involved table as  no data 

>if table has "NO data involved keyword" then ignore the table

>look for the next MSN involved Table ->if  entries present then retrieve it.

 

Problem : My code always gives me the very first table from the top  with MSN involved keyword. 

Please find the  attached test.docx

 

#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <Word.au3>

; Create application object
Local $oWord = _Word_Create()

; Open the test document
Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Test.docx", Default, Default, True)

;search for the number
$oRangeFound = _Word_DocFind($oDoc, "34-3330")
$oSearchRange = _Word_DocRangeSet($oDoc, $oRangeFound, $wdSection, -1, $wdWord, 1)
$oMSNFound = _Word_DocFind($oDoc, "MSN Involved",$oSearchRange,Default,True)

$oSearchRange1 = _Word_DocRangeSet($oDoc, $oMSNFound, $wdParagraph, 1, $wdParagraph, 3)
$oTables=$oSearchRange1.Tables
Local $asResult1 = _Word_DocTableRead($oDoc, $oTables(1), 1)
_ArrayDisplay($asResult1, "Word UDF: _Word_DocTableRead Example")

 

 

test.docx

Link to comment
Share on other sites

Wouldn’t be Excel the better tool to work with tables?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

Use function _Word_DocTableRead to read the whole table into an array and then do all further processing with this array.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

@water  by suggesting reading the whole  table into the array are you trying to say pull all the tables present in the word documents  into the array?  there are multiple Tables with the same name. Please refer the attached test  docx. i want table 1.2

I have used _WordDocTableRead and it work well but problem  is I'm  finding it difficult  to search for the immediate next table (Upwards) with table name MSN Involved  when "NO data involved " text is found in the table.

 

 

Link to comment
Share on other sites

The tables have an index. So in your document this is from 1 to 5. Read the needed tables into separate arrays and then work with those arrays.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

@water

Thank you.

Here is the working code.

$oSearchRange1 = _Word_DocRangeSet($oDoc, $oRangeFound, $wdSection, -1, $wdWord, 1)
;Search for MSN Involved
$oMSNFound = _Word_DocFind($oDoc, "MSN involved", $oSearchRange1,Default,False)
$oSearchRange = _Word_DocRangeSet($oDoc, $oMSNFound, $wdParagraph, 1, $wdParagraph, 3)
$oTables=$oSearchRange.Tables
Local $asMSN = _Word_DocTableRead($oDoc, $oTables(1), 1)
_ArrayDisplay($asMSN)

While $asMSN[2][0] == "NO data involved"
   $oMSNFound = _Word_DocFind($oDoc, "MSN involved", $oMSNFound,Default,False)
   $oSearchRange = _Word_DocRangeSet($oDoc, $oMSNFound, $wdParagraph, 1, $wdParagraph, 3)
   $oSearchRange.Select
   $oTables=$oSearchRange.Tables
   Local $asMSN = _Word_DocTableRead($oDoc, $oTables(1), 1)
   $oMSNFound = _Word_DocFind($oDoc, "MSN involved", $oMSNFound,Default,False)
   _ArrayDisplay($asMSN)
Wend

 

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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...