如果引用或轉貼,麻煩註明出處與本網誌連結,否則視為侵權。

2012年8月29日

AutoIT程式如何處理跳出式對話窗(popup dialog)

作者: Fred Wang (FW知識瑣記) 日期: 2012/8/29
AutoIT模擬Windows操作常發生跳出式對話窗(popup dialog)的處理,我整理了一個處理模式的範例,供大家參考

Local $strTitle = "==title=="           ; 要偵測的跳出對話窗的window title  
If WinWait($strTitle,"",3) <> 0  Then   ; 等待該對話窗出現(最多三秒)
   WinActivate($strTitle)    ; 將該對話窗變成最上層的active window
   WinWaitActive($strTitle,"",3)  ; 等待它變成active(最多三秒),才往下執行
   SendKeepActive($strTitle)  ; 在模擬鍵盤動作Send()過程,保持此對話窗為active,避免干擾
   If WinActive($strTitle) Then 
      Send("{ENTER}")     ; 送出鍵盤動作(依每個對話窗按鈕,有所不同)
      ...                 ; 或對話窗特有的動作
   EndIf
   WinWaitClose($strTitle,"",3) ; 等待該對話窗關閉(最多三秒),,才往下執行
EndIf

2012年8月28日

AutoIT程式怎樣讀取MS Word檔案中的表格內容

作者: 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

AutoIT程式怎樣讀取MS Word檔案的內容

作者: Fred Wang (FW知識瑣記) 日期: 2012/8/28

今天又開始寫AutoIT程式, 想要自動讀取,解析出MS Word檔案的內容, 網路上找的sample program is not work!  因此自己改寫, 測試成功, 可以提供網友參考!
採用AutoIT UDF word.au3


#cs ----------------------------------------------------------------------------
 AutoIt Version: 3.3.8.1
 Author:         Fred Wang (FW知識瑣記 http://fredwang.blogspot.tw ) 
 Script Function: Read MS Word's content using AutoIT UDF
 Date : 2012/8/28  
#ce ----------------------------------------------------------------------------
#include <word.au3>
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

if FileExists($fileName) Then
 $textOfWord = readWordDoc($fileName) ; Show the text from the document
 MsgBox( 1 ," The Content is : ", $textOfWord )
Else
 MsgBox( 1 ,"Message", $fileName & " Not Found!" )
Endif

Func readWordDoc($fileName)
 Local $oWordApp = _WordCreate($fileName,1,0)
 Local $oWordDocument = _WordDocGetCollection($oWordApp, 0)
 Local $oWordContent = $oWorddocument.Content ; Ask to Receive the Contents Object of the Object Document
 Local $TextDoc = $oWordContent.Text ; Ask to Extract the Text of the Contents Object in an AutoIt Variant
 _WordDocClose($oWordDocument)
 _WordQuit($oWordApp, -1)
 return $TextDoc
EndFunc