删除带有冒号的数字后面的回车符的宏

问题描述 投票:0回答:1
尝试修改在 Word 文档中在包含冒号的数字(例如 04:32)之前插入回车符的宏,以便删除包含冒号的数字后面的回车符。示例:

宏应该改变这一点: 02:41

发言者:好的。我们继续吧。

对此:

02:41 演讲者:好的。我们继续吧。

这是我正在尝试修改以实现此目的的宏:

Sub Insert_CR_Before_Number_With_Colon() Dim ParagraphCount As Long, BlankLineCount As Long Dim oDoc As Document Dim rng As Range, Y As Long Dim vbmFlags As VbMsgBoxStyle, vbmResult As VbMsgBoxResult, strMsg1 As String, strMsg2 As String Set oDoc = ActiveDocument If oDoc.Range.Characters.Count = 1 Then MsgBox "This document contains no text.", vbCritical, "Text check" Exit Sub End If strMsg1 = "This macro will insert one carriage return row before every occurrence of up to 4 TEXT numbers that contain a colon." & vbCr & vbCr _ & "The purpose of this macro is to make the active document easier to read but also to make it easier to apply outline or multilevel numbering (and styles) to it. It will look for and process only text numbers, not Word's outline or multilevel numbering." & vbCr & vbCr _ & "If the document already contains Word's numbering, the spacing can then be easily controlled through the number style, and there will be no need for this macro." & vbCr & vbCr _ & "Do you wish to proceed?" vbmFlags = vbYesNo vbmResult = MsgBox(strMsg1, vbmFlags, "Insert Carriage Return Before Number Containing a Colon") Select Case vbmResult Case vbNo Exit Sub End Select Set rng = oDoc.Range ParagraphCount = oDoc.Paragraphs.Count With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "([0-9][0-9]:[0-9])" .Replacement.Text = "^p\1" ' "^p\1" inserts CR BEFORE the number .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End Sub
我以为我可以改变

.Text = "([0-9][0-9]:[0-9])" .Replacement.Text = "^p\1" ' "^p\1" inserts CR BEFORE the number
查找带有冒号后跟回车符的数字的语句。我尝试过将 ^p 添加到 .Text 语句的各种版本,但没有任何效果。

vba ms-word
1个回答
0
投票
请尝试一下。

.Text = "([0-9]{1,2}:[0-9]{2})^13" .Replacement.Text = "\1 " ' replace ^p with a space
    
© www.soinside.com 2019 - 2024. All rights reserved.