【问题标题】:Loop through a sentence循环一个句子
【发布时间】:2015-09-24 16:33:18
【问题描述】:

我想找一个里面有蓝色文字的句子,选中整个句子,把蓝色的字一个一个去掉。我的代码只删除了句子中的第一个蓝色单词,然后才转到 With 语句。

我正在寻找类似这样的伪代码:
而 objSelectionChange.Sentences(1).Find.Font.Color = wdColorBlue
对于当前选定的句子,这将是一个嵌套的 while 循环

Do While True
objSelectionChange.Find.Forward = True
objSelectionChange.Find.Format = True
objSelectionChange.Find.Font.Color = wdColorBlue
objSelectionChange.Find.Execute
If objSelectionChange.Find.Found Then
    strg2 = objSelectionChange.Sentences(1).Text
    count = count + 1
    ReDim strgArray(count)
    strgArray(count) = objSelectionChange.Text
    MsgBox strgArray(count) & " Located In Array Index # " & count
    MsgBox strg2
    strg3 = Replace(strg2, strgArray(count), "")
    strg3 = Replace(strg3, "  ", " ")
    strg3 = Mid(strg3, 1, Len(strg3) - 2)
    MsgBox strg3
Else
    Exit Do
End If
    Set objRangeOrig = objDocOrig.Content
    '''''Search the string in the original manual'''''
    With objRangeOrig.Find
    .MatchWholeWord = False
    .MatchCase = False
    .MatchPhrase = True
    .IgnoreSpace = True
    .IgnorePunct = True
    .Wrap = wdFindContinue
    .Text = strg3
    .Replacement.Text = Left(strg2, Len(strg2) - 2)
    .Execute Replace:=wdReplaceOne
    End With
Loop

【问题讨论】:

    标签: vba loops ms-word do-while


    【解决方案1】:

    当您进行替换时,.Replacement.Text 只是一个没有格式的纯文本字符串。任何后续的蓝色单词都将替换为 color=Automatic 单词(可能是黑色)。这就是第一次替换后 Find 找不到任何东西的原因。

    这是一种无需查找和替换即可完成的方法。我认为它可以实现您想要的,但您可能需要根据您的情况对其进行调整。

    Dim rSearch As Range
    
    Set rSearch = ThisDocument.Range
    
    Do
        rSearch.Find.Forward = True
        rSearch.Find.Format = True
        rSearch.Find.Font.Color = wdColorBlue
        rSearch.Find.Execute
        If rSearch.Find.Found Then
            rSearch.Delete wdWord, 1
        Else
            Exit Do
        End If
    Loop
    

    【讨论】:

    • 如果我只需要删除单词,这将起作用,但我需要完全匹配 2 个字符串。这就是为什么我要删除蓝色单词。原始字符串是没有我要比较的蓝色单词的字符串。这对我来说很难说清楚,实际上我以前也没有运气问过这个问题。这是我的问题背后的整个故事的链接。我不得不尝试缩小我的问题范围以获得回应,你做到了。 stackoverflow.com/questions/32619642/…
    【解决方案2】:

    在循环内更新objSelectionChange,和第一次replace一样可以改变

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      相关资源
      最近更新 更多