找回密码
 加入
搜索
查看: 2105|回复: 3

[AU3基础] 批量处理word 时_WordCreate错误

[复制链接]
发表于 2011-3-3 13:26:41 | 显示全部楼层 |阅读模式
按如下代码批量处理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[0]-1]
For $i=0 To $filearray[0]-2
        $path[$i]=$filearray[1]&""&$filearray[$i+2]
Next;...........................................多选文件时得到每个文件的完整路径

_ArrayDisplay($path)
For $i=0 To $filearray[0]-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
发表于 2011-3-3 13:37:48 | 显示全部楼层
$o_object.visible = true
这样试一下,创建了word对象的话应该可以直接引用 word vba的内容了
 楼主| 发表于 2011-3-4 12:50:20 | 显示全部楼层
回复 2# kevinch


    谢谢帮助,我发现用$oWordApp = _WordCreate($path[$i],1,0),尝试附加到一个已存在的word窗口,这样也不会出错了,是有警告。不知道这又是什么原因
 楼主| 发表于 2011-3-4 12:50:40 | 显示全部楼层
回复 2# kevinch


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

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-15 14:09 , Processed in 0.072665 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表