假设我想使用VBA Word来查找/替换。我想找到用“页面”替换单词“page”(标题需要加粗“但如果前面的单词不是”继续下一个“,我只想这样做
我试图找到这个单词然后使用键命令来按住Ctrl + shift +左箭头和IF那些=“继续下一步”什么都不做,否则用“页面”替换“页面”(标题需要加粗)。 / p>
Sub SpellingSuggestionPage()
Dim wrd As Range
Dim srchText As String, avdText As String, replWord As String
Dim ar() As String
Dim ignoreWord As Boolean
srchText = "page"
avdText = "next"
replWord = "page (title needs bolded)"
ar = Split(avdText, " ")
For Each wrd In ActiveDocument.Words
ignoreWord = False
If wrd = srchText Then
If wrd.Previous(Unit:=wdWord, Count:=1).Text = avdText Or wrd.Previous(Unit:=wdWord, Count:=1).Bold Then
ignoreWord = True
End If
If ignoreWord = False Then
wrd.Text = replWord
End If
End If
Next
End Sub
答案 0 :(得分:1)
如果我理解正确,我认为这可能会这样做:
If InStr(value, "page") > 0 And InStr(value, "continued on next") = 0 Then
value = Replace(value, "page", "page (title needs bolded)")
End If
这就是说:如果值在任何地方都包含“page”而且在任何地方都不包含“next next”,那么将“page”替换为“page(title needs boldded)”。
答案 1 :(得分:1)
喜欢这个吗?
Option Explicit
Sub Sample()
Dim wrd As Range
Dim srchText As String, avdText As String, replWord As String
Dim ar() As String
Dim ignoreWord As Boolean
srchText = "page"
avdText = "continued on next"
replWord = "page (title needs bolded)"
ar = Split(avdText, " ")
For Each wrd In ActiveDocument.Words
ignoreWord = False
If wrd = srchText Then
If Trim(wrd.Previous(Unit:=wdWord, Count:=1).Text) = Trim(ar(2)) Then
If Trim(wrd.Previous(Unit:=wdWord, Count:=2).Text) = Trim(ar(1)) Then
If Trim(wrd.Previous(Unit:=wdWord, Count:=3).Text) = Trim(ar(0)) Then
ignoreWord = True
End If
End If
End If
If ignoreWord = False Then
wrd.Text = replWord
End If
End If
Next
End Sub