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

2012年8月28日

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

沒有留言:

張貼留言

歡迎提供意見, 謝謝 (註 : 留言經過版主審核通過才會發布)