【问题标题】:clean a Word document from multpile whitespace characters从多个空白字符中清除 Word 文档
【发布时间】:2016-02-03 22:15:50
【问题描述】:

我正在尝试清理 Word 中选定文本的每一行,以便删除任何不需要的字符(制表符、多个空格、多个换行符、空行)。

假设我有一个文本:

John        Smith 



John    Anderson Smith  

John           A. Smith     
J.A. Smith

我想实现以下目标:

John Smith 
John Anderson Smith 
John A. Smith 
J.A. Smith

让我向您展示我到目前为止所做的尝试。删除标签非常简单:

With Selection
    .Text = Replace(.Text, vbTab, "")
End With

换行符也是如此:

With Selection
    .Text = Replace(.Text, vbCrLf, ",")
End With

不幸的是,当我尝试使用 .Text = Replace(.Text, vbCrLf & vbCrLf, "") 删除两个换行符时,它无法正常工作 我也尝试了以下代码,但它也无法正常工作。

With Selection
    Do While InStr(1, .Text, vbCrLf & vbCrLf)
        .Text = Replace(.Text, vbCrLf & vbCrLf, vbCrlF)
    Loop
End With

我想出了另一段代码。我几乎成功了,即。删除制表符、前导空格和尾随空格以及两个以上的空格。但它也删除了 all 换行符,我只想删除重复的换行符(即多个)。正则表达式 \p{2,}\n 在这里不起作用。删除空行也很棒。

Sub test()
    With Selection
        Dim RegEx As Object
        Set RegEx = CreateObject("VBScript.RegExp")
        RegEx.Global = True
        RegEx.IgnoreCase = True
        RegEx.Pattern = "\s{2,}"
        .Text = Trim(RegEx.Replace(.Text, " "))
    End With
End Sub

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    试试

    Sub CleanUpPastedText()
        Application.ScreenUpdating = False
        With Selection.Range.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchAllWordForms = False
          .MatchSoundsLike = False
          .MatchWildcards = True
          'Eliminate spaces & tabs before paragraph breaks.
          .Text = "[ ^s^t]{1,}^13"
          .Replacement.Text = "^p"
          .Execute Replace:=wdReplaceAll
          'Replace single paragraph breaks with a space
          .Text = "([!^13])([^13])([!^13])"
          .Replacement.Text = "\1 \3"
          'Replace all double spaces with single spaces
          .Execute Replace:=wdReplaceAll
          .Text = "[ ]{2,}"
          .Replacement.Text = " "
          'Delete hypens in hyphenated text formerly split across lines
          .Execute Replace:=wdReplaceAll
          .Text = "([a-z])-[ ]{1,}([a-z])"
          .Replacement.Text = "\1\2"
          .Execute Replace:=wdReplaceAll
          'Limit paragraph breaks to one per 'real' paragraph.
          .Text = "[^13]{1,}"
          .Replacement.Text = "^p"
          .Execute Replace:=wdReplaceAll
        End With
        'Restore Screen Updating
        Application.ScreenUpdating = True
    End Sub
    

    编辑

    以上代码适用于 2007 年和 2010 年。

    对于 Office 16,您应该使用 ^013 而不是 ^13

    【讨论】:

    • 代码在.Execute Replace:=wdReplaceAll第一次出现时停止
    • 执行前是否选择了文本?对于word 2007,它工作正常。您使用的是哪个版本?
    • 是的,我是。我使用 Word 2016。明天我可以在 Word 2010 上签到,并且可能在 2013 上签到。
    • 这可能是原因。让我试试 2016。
    • @menteith 它在 2016 年对我有用:“[ ^s^t]{1;}^013” 注意 013,我添加了 0。
    【解决方案2】:

    我终于设法清理了文本。让我们考虑以下文本:

                    John              Smith
    
    
    
    
    John                    Anderson Smith
    John      A. Smith
    J.A. Smith
    
    
    
    
    
    
    <below many new line character, some lines with spaces, tabs>
    

    另一个答案中的代码:

          John Smith
    John                    Anderson Smith John A. Smith J.A. Smith
    

    这显然是不正确的。这是我的代码:

    With selection
            Dim RegEx As Object
            Set RegEx = CreateObject("VBScript.RegExp")
            RegEx.Global = True
            RegEx.Pattern = "[ \t]+"
            .Text = RegEx.Replace(.Text, " ")
            RegEx.MultiLine = True
            RegEx.Pattern = "^(?:[\t ]*(?:\r?\n|\r))+"
            .Text = RegEx.Replace(.Text, "")
            ' the following is from http://stackoverflow.com/a/24049145/2657875
            RegEx.Pattern = "^[\s\xA0]+|[\s\xA0]+$"
            .Text = RegEx.Replace(.Text, "")
    End With
    

    代码产生以下预期结果。

    Jim Kane
    John Andy Lemar
    J. A. Smith
    Jane Smith
    

    【讨论】:

    • 这里不需要正则表达式对象。通配符已经存在于 word 查找和替换中以执行您在此处执行的操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多