如果引用或轉貼,麻煩註明出處與本網誌連結,否則視為侵權。
顯示具有 AutoIT 標籤的文章。 顯示所有文章
顯示具有 AutoIT 標籤的文章。 顯示所有文章

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

2012年5月23日

怎樣用AutoIT播放影音檔

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

透過播放器如Windows Media Player, VLC, iTunes等自動播放影音,可以利用AutoIT的兩個Process Management指令ShellExecute與Run就可以達成

程式案例
$exeFile = "C:\Program Files\Windows Media Player\wmplayer.exe" ;播放器執行檔的路徑
$wavFile = "B:\BILD0982.WAV" ;影音檔的路徑
; 方法一 用ShellExecute
ShellExecute($exeFile,$wavFile)

; 方法二 用Run
$cmd = $exeFile & " " & $wavFile
Run($cmd) 

2012年2月6日

AutoIT應用-工作時間計算,整點休息提醒

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

下面小程式可以提供您工作時間計算與整點休息的提醒
include <date.au3>
HotKeySet("^!q", "Terminate")
HotKeySet("^!p", "Reset")
$begin = TimerInit()
Dim $nEnd = 0
Do
 $dif = TimerDiff($begin)
 $nSeconds = Round($dif / 1000, 0)   ;換成秒數
 $nMinutes = Floor($nSeconds / 60)   
 $nSecond = Mod($nSeconds , 60) 
 $nHours = Floor($nMinutes / 60)
 $nMinute = Mod($nMinutes , 60)  
 ToolTip("現在時間 : "&_NowTime()&@LF&"工作時間 : "&$nHours&"小時"&$nMinute&"分"&$nSecond&"秒",0,0,"Ctrl+Alt+q 結束,Ctrl+Alt+p 重新計時 (c)Fred Wang",1,4)  
 If  $nHours > 1 And $nMinute = 0 And $nSecond = 0 Then MsgBox(0,"Information","工作時間滿"&$nHours&"小時,休息一下吧!")  
 Sleep(1000)
Until $nEnd = 1

Func Terminate()
 $nEnd = 1
 ToolTip("") 
 Exit 0 
EndFunc

Func Reset()
 $begin = TimerInit()
EndFunc 

2012年2月2日

AutoIT - 透過免費的BlatMail傳送郵件

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

首先要設定BlatMail,需要指定SMTP server與送件者郵件信箱

程式 :
Func _SendBlatMail($pSubject,$pBody,$pMailAddresses)
 Local $sMailExec = ""
 Local $sBlatPath = IniRead("Config.ini", "BlatMail", "Path", "NotFound")

 If $sBlatPath <> "NotFound" And $pMailAddresses <> "" Then
  $sMailExec = $sBlatPath & "blat.exe" 
  $aAddress = StringSplit($pMailAddresses, ",")  ;以逗號區隔收件人
  For $i = 1 To $aAddress[0] ; 可能有多個號碼
   _SendMail($pSubject,$pBody,$aAddress[$i],$sMailExec) ;傳送Mail
  Next
 EndIf 
EndFunc 

Func _SendMail($pSubject, $pBody, $sendTo,$sMailExec)
 ; Note: -charset big5 傳送中文
 Local $sParm = " - -body """ & $pBody & """ -subject """ & $pSubject & """ -to " & $sendTo & " -charset big5" 
 ShellExecute($sMailExec, $sParm, @ScriptDir, "", @SW_HIDE) ;hide blat console 
EndFunc 

2012年1月31日

AutoIT怎樣開啟IE的Tab?

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

終於在autoit的論壇網站中找到解答,原來還有Navigate2這個功能,但是在官方的Help中並沒有這個功能的說明

#include <IE.au3> Const $navOpenInNewTab = 0x0800 Dim $o_IE = _IECreate('http://www.google.com/') $o_IE.Navigate2('http://www.yahoo.com/', $navOpenInNewTab) $o_IE.Navigate2('http://www.autoitscript.com/', $navOpenInNewTab)