word查询替换的诡异情况(已结)
本帖最后由 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])
Else
ExitLoop
EndIf
WEnd
.text = "[+B+]"
While 1
.execute
If .found Then
.parent.typetext($data[$row])
Else
ExitLoop
EndIf
WEnd
EndWith
我的代码就是为了实现从excel读取数据,然后替换到word中。Local $xls = _ExcelBookOpen(@ScriptDir & "\data.xls")
$data = _ExcelReadSheetToArray($xls)
_ExcelBookClose($xls, 0)
用这个代码读取,都很正常。但是替换的时候,[+A+]或者[+B+]是数字的话,就会失败 楼主的写复杂了,而且估计效率不太好,试下下面这个,可以自己加上些提示方式$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 诡异??诡异?? 楼主的写复杂了,而且估计效率不太好,试下下面这个,可以自己加上些提示方式
kevinch 发表于 2015-1-16 19:37 http://www.autoitx.com/images/common/back.gif
谢谢,非常感谢!!!
页:
[1]