Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Word VBA:如何选择找到的文本而不是光标所在的位置_Vba_Find_Ms Word - Fatal编程技术网

Word VBA:如何选择找到的文本而不是光标所在的位置

Word VBA:如何选择找到的文本而不是光标所在的位置,vba,find,ms-word,Vba,Find,Ms Word,这可能很简单,但我无法让它工作 我需要搜索我的文档,找到包含字符串“alog”的单词并添加“ue”。例如,“目录”-->“目录” 上面的方法很好,但我无法进行下一步工作:如果找到的字符串在“log”之后已经有了“ue”,我不想再添加一个“ue” 从宏访问的子例程如下所示。我试着在“while execute”部分中添加以下行,但是“selection”总是恰好是光标所在的单词 With Selection .Expand unit:=wdWord End With I)如何选择找到

这可能很简单,但我无法让它工作

我需要搜索我的文档,找到包含字符串“alog”的单词并添加“ue”。例如,“目录”-->“目录”

上面的方法很好,但我无法进行下一步工作:如果找到的字符串在“log”之后已经有了“ue”,我不想再添加一个“ue”

从宏访问的子例程如下所示。我试着在“while execute”部分中添加以下行,但是“selection”总是恰好是光标所在的单词

With Selection

    .Expand unit:=wdWord

End With
I)如何选择找到范围的内容,以及ii)如何将新选择扩展两个字符,以查看这两个字符是否为“ue”

非常感谢

    Sub do_replace2(old_text As String, new_text As String, Count_changes As Integer)

    ' Replaces 'log' with 'logue'
    ' Ignores paragraphs in styles beginning with 'Question'

    Dim rg As Range
    Set rg = ActiveDocument.Range

    With rg.Find
    .Text = old_text
    While .Execute
        If Left(rg.Paragraphs(1).Style, 8) <> "Question" Then
            rg.Text = new_text
                With ActiveDocument.Comments.Add(rg, "Changed from '" & old_text & "'")
                .Initial = "-logs"
                .Author = "-logs"
                End With
                Count_changes = Count_changes + 1
           End If
     rg.Collapse wdCollapseEnd
    Wend
    End With
    End Sub
Sub do_replace2(旧_文本为字符串,新_文本为字符串,计数_更改为整数)
'将'log'替换为'logue'
'忽略以'Question'开头的样式的段落'
变暗rg As范围
Set rg=ActiveDocument.Range
与rg.Find
.Text=旧文本
执行
如果左(参见第(1)款风格,8)“问题”,则
rg.Text=新文本
使用ActiveDocument.Comments.Add(rg,“已从“&”旧文本&“”)更改)
.Initial=“-logs”
.Author=“-logs”
以
计数更改=计数更改+1
如果结束
rg.塌陷和塌陷
温德
以
端接头

我不太确定我是否遵循了您问题的第一部分“如何选择找到范围的内容”。
rg
变量已包含搜索结果。如果要选择它,只需使用
rg。选择
。这在调试中可能很有用(这样,当您在代码中单步执行时,您可以看到
范围的位置),但实际上没有任何其他理由在您的问题的代码中使用
选择
对象。您只需使用
范围
对象即可

至于您问题的第2部分“如何…将新选择扩展两个字符”,您只需将2添加到
范围的
.End
属性中即可。由于您仅将其用于测试(并且由于
.Find
方法可能不可靠),请在
rg
的副本上进行测试:

With rg.Find
    .Text = old_text
    While .Execute
        If Left(rg.Paragraphs(1).Style, 8) <> "Question" Then
            Dim test As Range
            Set test = rg.Duplicate         'copy the found Range.
            test.Collapse wdCollapseEnd     'move to the end of it.
            test.End = test.End + 2         'expand to the next 2 characters.
            If test.Text <> "ue" Then       'see if it's "ue".
                rg.Text = new_text
                With ActiveDocument.Comments.Add(rg, "Changed from '" & old_text & "'")
                    .Initial = "-logs"
                    .Author = "-logs"
                End With
                Count_changes = Count_changes + 1
            End If
        End If
        rg.Collapse wdCollapseEnd
    Wend
End With
带有rg.Find的

.Text=旧文本
执行
如果左(参见第(1)款风格,8)“问题”,则
作为范围的暗淡测试
设置test=rg.Duplicate'复制找到的范围。
测试。折叠WDCOLLAPSEND'移动到它的末尾。
test.End=test.End+2'扩展到下两个字符。
如果测试。文本“ue”,则“查看它是否为“ue”。
rg.Text=新文本
使用ActiveDocument.Comments.Add(rg,“已从“&”旧文本&“”)更改)
.Initial=“-logs”
.Author=“-logs”
以
计数更改=计数更改+1
如果结束
如果结束
rg.塌陷和塌陷
温德
以

感谢共产国际;正如我所说的,end+2可以很好地工作,给了我一个范围(比如)735的开始和737的结束,其中有一个字符串。但是如果我执行.wdcollapsestart和.start+5,则范围start和end相同,字符串为null。为什么“开始”与“结束”的工作方式不同?另一个更新:当我这样做时:test.collapsestart test.start=test.start-5它工作正常。你不能做“开始+n”吗?