Jump to content
Sign in to follow this  
gfunk999

Word.au3 How to read a Table into a Variable

Recommended Posts

gfunk999

Hi all,

Is there a way to read a table in Word and pass it to a variable (i.e. $oTable)? For example, Big Daddy's script ($oTable = _WordDocTableAdd($oDoc, $oRange, 3, 2, True)) below works perfect if the table does not exist, but what if the table already exist, is there an object/method that can be use to read the table and then pass it to a variable?

Thank you,

Gary

#include <Word.au3>

Dim $aQuestions[3] = ["Name", "Address", "Telephone Number"]

$oWordApp = _WordCreate(@ScriptDir & "\Test.doc", 1, 0)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oRange = $oDoc.Range
$oTable = _WordDocTableAdd($oDoc, $oRange, 3, 2, True)
$oCells = $oTable.Range.Cells
$x = 0
For $i = 1 To $oCells.Count
    If Mod($i, 2) = 0 Then
        $sAnswer = InputBox("Question", "What is your " & $aQuestions[$x] & "?")
        $oCells ($i).Range.InsertAfter ($sAnswer)
        $x += 1
    Else
        $oCells ($i).Range.InsertAfter ($aQuestions[$x])
    EndIf
Next

_WordPropertySet($oWordApp, "visible", True)

For $i = 0 To 42
    $oTable.AutoFormat ($i, True, True, True)
    Sleep(500)
Next

;===============================================================================
;
; Function Name:    _WordDocTableAdd()
; Description:      Returns an object variable representing a new table object
; Parameter(s):     $o_object            - Object variable of a Word.Application, document object
;               $o_range            - The range where you want the table to appear. The table replaces the range, if the range isn't collapsed
;               $i_NumRows    - The number of rows you want to include in the table
;               $i_NumColumns      - The number of columns you want to include in the table
;               $i_DefaultTableBehavior    - Optional: Sets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells contents (AutoFit)
;                                    0 = (Default) AutoFit disabled
;                                    1 = AutoFit enabled
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success  - Returns an object variable pointing to a Word.Application, table object
;                   On Failure  - Returns 0 and sets @ERROR
;               @ERROR  - 0 ($_WordStatus_Success) = No Error
;                        - 1 ($_WordStatus_GeneralError) = General Error
;                        - 3 ($_WordStatus_InvalidDataType) = Invalid Data Type
;                        - 4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
;               @Extended  - Contains invalid parameter number
; Author(s):        Bob Anthony (Code based off IE.au3)
;
;===============================================================================
;
Func _WordDocTableAdd(ByRef $o_object, $o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior = 0)
    If Not IsObj($o_object) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidDataType")
        SetError($_WordStatus_InvalidDataType, 1)
        Return 0
    EndIf
    ;
    If Not __WordIsObjType ($o_object, "document") Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "$_WordStatus_InvalidObjectType")
        SetError($_WordStatus_InvalidObjectType, 1)
        Return 0
    EndIf
    ;
    Local $o_table

    $o_table = $o_object.Tables.Add ($o_range, $i_NumRows, $i_NumColumns, $i_DefaultTableBehavior)
    If Not IsObj($o_table) Then
        __WordErrorNotify ("Error", "_WordDocTableAdd", "", "Tables Object Creation Failed")
        SetError($_WordStatus_GeneralError)
        Return 0
    EndIf

    SetError($_WordStatus_Success)
    Return $o_table
EndFunc   ;==>_WordDocTableAdd


--------------------
Edited by gfunk999

Share this post


Link to post
Share on other sites
big_daddy

See if this is what you are looking for.

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

#AutoIt3Wrapper_Au3Check_Parameters = -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

Global $oWordApp, $oDoc, $oTable, $aTable

_WordErrorHandlerRegister()
$oWordApp = _WordCreate("C:\Temp\Test.docx", 1)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oTable = $oDoc.Tables(1)
$aTable = _WordDocTableWriteToArray($oTable, True)
_ArrayDisplay($aTable)

; #FUNCTION# ;===============================================================================
;
; Name...........: _WordDocTableWriteToArray()
; Description ...: Reads the contents of a Table into an array
; Syntax.........: _WordDocTableWriteToArray(ByRef $o_object, $f_transpose = False)
; Parameters ....: $o_object - Object variable of a Word.Application, Table object
;                  $f_transpose - Boolean value.  If True, swap rows and columns in output array
; Return values .: Success - Returns a 2-dimensional array containing the contents of the Table
;                  Failure - Returns 0 and sets @ERROR
;                  |0 ($_WordStatus_Success) = No Error
;                  |3 ($_WordStatus_InvalidDataType) = Invalid Data Type
;                  |4 ($_WordStatus_InvalidObjectType) = Invalid Object Type
;                  @Extended  - Contains invalid parameter number
; Author ........: Bob Anthony (big_daddy)
; Modified.......:
; Remarks .......:
;
; ;==========================================================================================
Func _WordDocTableWriteToArray(ByRef $o_object, $f_transpose = False)
    If Not IsObj($o_object) Then
        __WordErrorNotify("Error", "_WordDocTableWriteToArray", "$_WordStatus_InvalidDataType")
        SetError($_WordStatus_InvalidDataType, 1)
        Return 0
    EndIf
    ;
;~  If Not __WordIsObjType($o_object, "table") Then
;~      __WordErrorNotify("Error", "_WordDocTableWriteToArray", "$_WordStatus_InvalidObjectType")
;~      SetError($_WordStatus_InvalidObjectType, 1)
;~      Return 0
;~  EndIf
    ;
    Local $i_cols, $trs, $tr, $tds, $i_rows, $col, $row
    $tds = $o_object.columns
    $trs = $o_object.rows
    $i_cols = $tds.count
    $i_rows = $trs.count
    Local $a_TableCells[$i_cols][$i_rows]
    $row = 0
    For $tr In $trs
        $tds = $tr.cells
        $col = 0
        For $td In $tds
            $a_TableCells[$col][$row] = StringReplace(StringStripCR($td.Range.Text), Chr(7), "")
            $col += 1
        Next
        $row += 1
    Next
    If $f_transpose Then
        Local $i_d1 = UBound($a_TableCells, 1), $i_d2 = UBound($a_TableCells, 2), $aTmp[$i_d2][$i_d1]
        For $i = 0 To $i_d2 - 1
            For $j = 0 To $i_d1 - 1
                $aTmp[$i][$j] = $a_TableCells[$j][$i]
            Next
        Next
        $a_TableCells = $aTmp
    EndIf
    SetError($_WordStatus_Success)
    Return $a_TableCells
EndFunc   ;==>_WordDocTableWriteToArray

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.