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

[效率算法] word查询替换的诡异情况(已结)

[复制链接]
发表于 2015-1-16 17:38:04 | 显示全部楼层 |阅读模式
本帖最后由 gift8188 于 2015-1-16 22:16 编辑
        $word = ObjCreate("word.application")
                $word.visible = True
                $doc = $word.documents.open(@ScriptDir & "\word.doc")
                With $doc.parent.selection.find
                        .clearformatting
                        .replacement.clearformatting
                        .forward = 1
                        .wrap = 0
                        
                                .text = "[+A+]"
                                While 1
                                        .execute
                                        If .found Then
                                                .parent.typetext($data[$row][1])
                                        Else
                                                ExitLoop
                                        EndIf
                                WEnd

                                .text = "[+B+]"
                                While 1
                                        .execute
                                        If .found Then
                                                .parent.typetext($data[$row][2])
                                        Else
                                                ExitLoop
                                        EndIf
                                WEnd
                        EndWith
                        
我的代码就是为了实现从excel读取数据,然后替换到word中。
Local $xls = _ExcelBookOpen(@ScriptDir & "\data.xls")
        $data = _ExcelReadSheetToArray($xls)
        _ExcelBookClose($xls, 0)
用这个代码读取,都很正常。但是替换的时候,[+A+]或者[+B+]是数字的话,就会失败

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2015-1-16 19:37:57 | 显示全部楼层
楼主的写复杂了,而且估计效率不太好,试下下面这个,可以自己加上些提示方式
$xls=ObjGet(@ScriptDir&"\data.xls")
With $xls.activesheet
        $rng=.range("a1",.cells(1,.columns.count).end(1))
EndWith
$word=ObjCreate("word.application")
$word.visible=False  ;想看到替换过程的话,把这里改成true
For $r In $rng
        If StringLen($r.value)>0 Then
                $doc=$word.documents.open(@ScriptDir&"\word.doc")
                With $doc
                        .parent.selection.find.execute($r.value, Default, Default, Default, Default, Default, Default, Default, Default, $r.offset(1).value, 2)
                        .saveas(@ScriptDir&""&$r.column&"-"&$r.value&".doc")
                        .close
                EndWith
        EndIf
Next
$xls.close(false)
$word.quit
发表于 2015-1-16 21:09:04 | 显示全部楼层
诡异??诡异??
 楼主| 发表于 2015-1-16 22:15:09 | 显示全部楼层
楼主的写复杂了,而且估计效率不太好,试下下面这个,可以自己加上些提示方式
kevinch 发表于 2015-1-16 19:37



    谢谢,非常感谢!!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-5 18:19 , Processed in 0.086928 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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