WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

OLE with Word

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Extract Data from a Table in Word


Extracts data from tables in Word document and writes to flat text file
file = "f:\myWord.doc"
output = "f:\myText.txt"
ofile = FileOpen(output,"WRITE")
oWord  = ObjectOpen("Word.Application")
oDOCS = oWord.Documents
oDOC = oDOCS.Open(file)
oWord.visible=@TRUE

oActiveDoc = oWord.activedocument
ADR = oActiveDoc.Range                        ;<-- create a RANGE reference...

ADT = oActiveDoc.Tables

tabls = ADT.count                                       ;get the total number of tables in the word document

For i = 1 To tabls
   mytable = ADR.Tables(i)
        Tabr = mytable.Range                            ; set range to curent table
        nrRows = Tabr.Rows.Count                        ; get number of rows in current table
        nrCols = Tabr.Columns.Count                     ; get number of columns in current table
   For x = 1 To nrRows
      cText1 = mytable.Cell(x,1).Range.Text    ; get row x col 1 value
      cText2 = mytable.Cell(x,3).Range.Text    ; get row x col 3 value
      cText3 = mytable.Cell(x,4).Range.Text    ; get row x col 4 value
      oText = StrCat(cText1," ",cText2," ",cText3)
      FileWrite(ofile,oText)
   Next x
Next i
Message("Table Extraction", "complete" )

; Clean up

FileClose(ofile)
ObjectClose(mytable)
ObjectClose(ADT)
ObjectClose(ADR)
ObjectClose(oActiveDoc)
ObjectClose(oDOCS)
ObjectClose(oWord)

Exit

Article ID:   W17185
File Created: 2007:07:03:14:28:38
Last Updated: 2007:07:03:14:28:38