【发布时间】:2021-01-22 06:20:15
【问题描述】:
我正在尝试通过宏从 Word 文本中删除突出显示的空白字符,但只要遇到一些 cmets 或 URL(不是全部),它就会挂起/循环。这怎么可能?解决方案是什么?
Sub checkforHighlightsOrg()
Application.ScreenUpdating = False
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.Pattern = "^\s+$" ' highlighted text having multiple white-space/invisible chars only
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Replacement.Highlight = True
.Replacement.ClearFormatting
End With
Dim bReplaced As Boolean
bReplaced = False
Do While Selection.Find.Execute = True
If Selection.Find.Found Then
If regex.Test(Selection.text) Then
bReplaced = True
Selection.text = regex.Replace(Selection.text, "")
End If
End If
DoEvents
Loop
If bReplaced Then MsgBox "Highlighted white-spaces have been removed."
Set rngTemp = ActiveDocument.Range
With rngTemp.Find
.ClearFormatting
.Highlight = True
.Forward = True
.Execute
End With
If rngTemp.Find.Found = True Then
MsgBox ("There have been non-white-space highlights found.")
End If
Application.ScreenUpdating = True
End Sub
我试过的另一个版本如下:
Sub checkforHighlightsV2()
Application.ScreenUpdating = False
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = True
regex.Pattern = "\s+" ' highlighted text having multiple white-space/invisible chars only
ActiveDocument.Select
Dim regex2 As Object, str As String
Set regex2 = CreateObject("VBScript.RegExp")
With regex2
.Pattern = "\s"
.Global = True 'If False, would replace only first
End With
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Replacement.Highlight = True
.Replacement.ClearFormatting
End With
Dim bReplaced As Boolean
bReplaced = False
Dim a As Range
restart:
Do While Selection.Find.Execute = True
If Selection.Find.Found Then
'Selection.MoveEnd wdParagraph, 1
Set a = Selection.Range.Duplicate
'Debug.Print Asc(a.text)
'a.MoveEnd wdCharacter, -1
Dim res As String
If regex.Replace(Replace(a.text, Chr(160), ""), "") = "" Then
Debug.Print "empty"
Selection.Delete
End If
' If a.text = vbCr Or a.text = vbLf Or a.text = vbCrLf Or a.text = vbNewLine Or a.text = vbTab Then
' ' Debug.Print "newline"
' bReplaced = True
' Selection.Delete
' GoTo restart
'
' End If
' If a.text = " " Then Selection.Delete
'
''
'' If regex.Test(a.text) Then
'' bReplaced = True
'' 'a.text = regex.Replace(a.text, "")
'' Selection.Delete
''
'' End If
End If
DoEvents
Loop
If bReplaced Then MsgBox "Highlighted white-spaces have been removed."
Set rngTemp = ActiveDocument.Range
With rngTemp.Find
.ClearFormatting
.Highlight = True
.Forward = True
.Execute
End With
If rngTemp.Find.Found = True Then
MsgBox ("There have been non-white-space highlights found, this usually means default text.")
End If
Application.ScreenUpdating = True
End Sub
我最初认为我不应该在搜索选择处于活动状态时替换文本,所以我尝试通过创建版本 2 并调用 selection.delete 来修复它,但不知何故它也不起作用。
正常的搜索对话框不会永远循环,但不允许出现空白字符。 感谢您的帮助。
编辑:我也尝试了这个(仅删除突出显示;不删除空格,当我按 Enter 插入文本时突出显示换行符/新段落时 - 这表明标记/突出显示在换行符/段落字符上处于活动状态 - 我尝试了一些变体,例如 ^w^p 但当我想使用 OR 运算符时,我不能将它与“使用通配符”选项结合使用)
Sub Macro6()
'
' Macro6 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = False
With Selection.Find
.Text = "^w"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
最新版本:
Sub RemoveHighlightedWhiteSpace()
Application.ScreenUpdating = False
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Highlight = True
.Replacement.Highlight = False
.text = "[ ,^9,^11,^12,^13," & Chr(160) & "," & Chr(164) & "]{2,}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
您是否尝试过在 Word 中使用 ^w 作为查找文本进行高级查找?
-
是的,我试过了,我用这个结果编辑了帖子。
-
^w 将找到制表符、空格、不间断空格和 1/4 em 空格。将替换文本留空将删除这些字符,而不仅仅是删除突出显示。具体来说,您希望替换哪些字符?
-
它删除了突出显示,但是当我将鼠标/光标水平指向页面中间时(在左侧,它以前是黄色突出显示的)它停留在那里闪烁,这表明空替换确实没有发生?
-
它在 O365 中为我工作。具体来说,您希望替换哪些字符?
标签: vba loops replace ms-word selection