VBA 关于检测是否有指定字符。
整个过程:光标到达指定字符,并向右移2个字符。再向右选取每一个字符,有指定的字符,则光标再向右移动4个字符,同时退出for。
问题:怎么没有移动光标呢?谢谢各位看官,本人没有办法了,请求帮助。
Option Explicit
Dim i As Integer
Private Sub Command1_Click()
Dim wordApp
Dim Word
Set wordApp = CreateObject("Word.Application")
Set Word = wordApp.Documents.Open(App.Path & "\施工收费核定清单(表4)(模板).doc")
wordApp.Visible = True
Dim rngTenCharacters As Range '设置
wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=2 '则光标向右移动2个字符
For i = 0 To 100 '从第三个字开始,至第100个字
wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=1 '光标向右移动一个字符
Set rngTenCharacters = wordApp.ActiveDocument.Range(Start:=i, End:=i + 1) '每次选取中一个字符
'MsgBox rngTenCharacters '显示该字符是什么字符
If rngTenCharacters = "标" Then '如果是“标”
wordApp.Selection.MoveRight Unit:=wdCharacter, Count:=4 '则光标向右移动四个字符
Exit For '退出for循环
End If
Next i
wordApp.Selection.TypeText Text:="一草一木" '退出后再写入字符
End Sub
[解决办法]
For i = 424 To 451 '从第0个字开始,至第500个字
wdApp.ActiveDocument.Range(start:=i, End:=i + 6).Select '显示该字符是什么字符
If wdApp.ActiveDocument.Range(start:=i, End:=i + 6).Text = "联系电话: " Then
wdApp.Selection.MoveRight Unit:=wdCharacter, Count:=1 '光标向右移动一个字符
Exit For
End If
Next i
wdApp.Selection.TypeText Text:=Text58.Text
Call CharNumber(Trim(Text58.Text))
If otherChar = 0 Then
ChinaEn = Val(NumberChar) '- 1
Else
ChinaEn = Round(3 * Val(otherChar) / 2) + Val(NumberChar) + 1
End If
wdApp.Selection.MoveRight Unit:=wdCharacter, Count:=0 '光标至Text尾
For i = 1 To ChinaEn
wdApp.Selection.Delete Unit:=wdCharacter, Count:=1 '删除一格
Next i