papapa314 发表于 2011-3-3 13:26:41

批量处理word 时_WordCreate错误

按如下代码批量处理word时总是连续处理几个文档后程序就出错退出,出错的位置是_WordCreate(已用红色标出),不知原因,请教各位!!tks in advance!#include<file.au3>
#include<array.au3>
#include <Word.au3>

$file=FileOpenDialog("","","(*.doc)",4)
;MsgBox(0,0,$file)
$filearray=StringSplit($file,"|")
;_ArrayDisplay($filearray)
Dim $path[$filearray-1]
For $i=0 To $filearray-2
        $path[$i]=$filearray&"\"&$filearray[$i+2]
Next;...........................................多选文件时得到每个文件的完整路径

_ArrayDisplay($path)
For $i=0 To $filearray-2
;MsgBox(0,0,$path[$i])

$oWordApp = _WordCreate($path[$i],0)
$oWordDoc = _WordDocGetCollection($oWordApp, 0)

$oWordDoc.Range.insertBefore ("(parapa314)");在文档开头加入一段话


$oWordDoc.ActiveWindow.ActivePane.View.SeekView   =   1;   //激活页眉的编辑   

;$sHeader = $oWordApp.Selection.HeaderFooter.Range.Text; //读出页眉文本

;MsgBox(0,0,$oWordApp.Application.System.FreeDiskSpace)

;MsgBox(0,0,$oWordApp.Selection.Sentences.Item(1))

;$oWordApp.ActiveDocument.Range.Words(1).InsertAfter("papapa314");在文档的第一个词后加入括号内的词

;$oWordApp.ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter;第一段话后加入一段话

$oWordApp.Selection.HeaderFooter.Range.Text =("papapa314"); //替换页眉为新文本

;$oWordApp.ActiveDocument.Range.Paragraphs(1).InsertAfter("的房间按金额空间");第一段话后加入字符

;$oWordDoc.ActiveWindow.ActivePane.View.SeekView   =   0;

$oWordApp.ActiveDocument.Sections.Last.Range.InsertAfter("papapa314");文档后面加语句

$oWordApp.ActiveDocument.Content.Font.Name =("宋体");把所有字体改为宋体

_WordQuit ($oWordApp, -1)



Next

Func _WordCreate($s_FilePath = "", $f_tryAttach = 0, $f_visible = 1, $f_takeFocus = 1)
        If Not $f_visible Then $f_takeFocus = 0 ; Force takeFocus to 0 for hidden window
        If $s_FilePath = "" Then $f_tryAttach = 0 ; There is currently no way of attaching to a blank document

        If $f_tryAttach Then
                Local $o_Result = _WordAttach($s_FilePath)
                If IsObj($o_Result) Then
                        If $f_takeFocus Then
                                Local $o_win = $o_Result.ActiveWindow
                                Local $h_hwnd = __WordGetHWND($o_win)
                                If IsHWnd($h_hwnd) Then WinActivate($h_hwnd)
                        EndIf
                        Return SetError($_WordStatus_Success, 1, $o_Result)
                EndIf
        EndIf

        Local $result, $f_mustUnlock = 0, $i_ErrorStatusCode = $_WordStatus_Success
        If Not $f_visible Then
                $result = __WordLockSetForegroundWindow($WORD_LSFW_LOCK)
                If $result Then $f_mustUnlock = 1
        EndIf

        ; Setup internal error handler to Trap COM errors, turn off error notification
        Local $status = __WordInternalErrorHandlerRegister()
        If Not $status Then __WordErrorNotify("Warning", "_WordCreate", _
                        "Cannot register internal error handler, cannot trap COM errors", _
                        "Use _WordErrorHandlerRegister() to register a user error handler")
        Local $f_NotifyStatus = _WordErrorNotify() ; save current error notify status
        _WordErrorNotify(False)

        Local $o_object = ObjGet("", "Word.Application")
        If Not IsObj($o_object) Or @error = $_WordStatus_ComError Then
                $i_ErrorStatusCode = $_WordStatus_NoMatch
        EndIf

        ; restore error notify and error handler status
        _WordErrorNotify($f_NotifyStatus) ; restore notification status
        __WordInternalErrorHandlerDeRegister()

        If Not $i_ErrorStatusCode = $_WordStatus_Success Then
                $o_object = ObjCreate("Word.Application")
                If Not IsObj($o_object) Then
                        __WordErrorNotify("Error", "_WordCreate", "", "Word Object Creation Failed")
                        Return SetError($_WordStatus_GeneralError, 0, 0)
                EndIf
        EndIf

        $o_object.visible = $f_visible
        If $f_mustUnlock Then
                $result = __WordLockSetForegroundWindow($WORD_LSFW_UNLOCK)
                If Not $result Then __WordErrorNotify("Warning", "_WordCreate", "", "Foreground Window Unlock Failed!")
                ; If the unlock doesn't work we will have created an unwanted modal window
        EndIf

        If $s_FilePath = "" Then
                _WordDocAdd($o_object)
        Else
                _WordDocOpen($o_object, $s_FilePath)
        EndIf
        Return SetError(@error, 0, $o_object)
EndFunc   ;==>_WordCreate

kevinch 发表于 2011-3-3 13:37:48

$o_object.visible = true这样试一下,创建了word对象的话应该可以直接引用 word vba的内容了

papapa314 发表于 2011-3-4 12:50:20

回复 2# kevinch


    谢谢帮助,我发现用$oWordApp = _WordCreate($path[$i],1,0),尝试附加到一个已存在的word窗口,这样也不会出错了,是有警告。不知道这又是什么原因

papapa314 发表于 2011-3-4 12:50:40

回复 2# kevinch


    谢谢帮助,我发现用$oWordApp = _WordCreate($path[$i],1,0),尝试附加到一个已存在的word窗口,这样也不会出错了,是有警告。不知道这又是什么原因
页: [1]
查看完整版本: 批量处理word 时_WordCreate错误