如何在MS-Word中删除具有不同格式的段落标记

时间:2019-06-24 22:19:26

标签: vba ms-word

我有几行用段落标记分隔,但实际上它们属于同一段落。

问题在于文本以Arial 15斜体显示,但是某些单词不是斜体(以红色显示),段落标记是Arial 15斜体,不是斜体甚至是Calibri11。这意味着,并非所有段落标记都具有相同的格式。

我要用空格“”替换段落标记,以便仅将Arial斜体字为15的那些连续行加入到单个段落中 即使其中没​​有斜体字。

我到目前为止尝试过:

查找内容:^ p(格式:Arial 15,斜体) 替换为:““

但是这样做只能找到绿色显示的段落标记。

下面是输入格式的图像以及我要获取的输出。另外,我还附有示例文件Sample.docx

Sample Image 感谢您的帮助

1 个答案:

答案 0 :(得分:2)

尝试

Option Explicit
Sub ReplacePara()
Dim Para As Paragraph, Xstr As String, Rng As Range
Dim i As Long, ln As Long
Dim PrvChrSize As Integer, NextChrSize  As Integer
Dim PrvChrFont As String, NextChrFont  As String
Dim PrvChrItalic As Boolean, NextChrItalic As Boolean

    With ActiveDocument
    For i = .Paragraphs.Count To 1 Step -1
    Set Para = .Paragraphs(i)
    ln = Para.Range.Characters.Count

        If ln > 1 Then
            With Para.Range.Characters(ln - 1).Font
            PrvChrSize = .Size
            PrvChrFont = .Name
            PrvChrItalic = .Italic
            End With

            If i < .Paragraphs.Count Then
                With .Paragraphs(i + 1).Range.Characters(1).Font
                NextChrSize = .Size
                NextChrFont = .Name
                NextChrItalic = .Italic
                End With
            Else
            NextChrSize = 0
            NextChrFont = ""
            NextChrItalic = False
            End If
        End If

        Debug.Print i, PrvChrSize, PrvChrFont, NextChrSize, NextChrFont
        If (PrvChrSize = 15 And (PrvChrFont = "Arial" Or PrvChrItalic = True)) _
        And (NextChrSize = 15 And (NextChrFont = "Arial" Or NextChrItalic)) Then
        Para.Range.Characters(ln).Text = " "
        End If
    Next
  End With

End Sub

示例文件的结果

enter image description here