作者: Fred Wang (FW知識瑣記) 日期: 2012/8/28
在AutoIT論壇中找到一篇非常好的範例, 將word中的表格讀入array中
http://www.autoitscript.com/forum/topic/94390-wordau3-how-to-read-a-table-into-a-variable/
其中網友big_daddy(Bob Anthony)寫的
稍加改寫前面幾行, 測試OK! 如下
#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
Global $fileName = FileOpenDialog("Please select a file.", @ScriptDir & "\", "Images (*.doc;*.docx)", 1 + 4)
If @error Then
MsgBox(4096, "", "No File(s) chosen")
Else
$fileName = StringReplace($fileName, "|", @CRLF)
EndIf
ConsoleWrite($fileName & @LF)
Global $nthOfTable = InputBox("Pasring Word Doc","Which table will you get(1,2,3...)")
if FileExists($fileName) Then
_WordErrorHandlerRegister()
$oWordApp = _WordCreate($fileName, 1, 0)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$oTable = $oDoc.Tables($nthOfTable)
$aTable = _WordDocTableWriteToArray($oTable, True)
_ArrayDisplay($aTable)
_WordDocClose($oDoc)
_WordQuit($oWordApp)
Else
MsgBox( 1 ,"Message", $fileName & " Not Found!" )
Endif
; #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