Microsoft Word VBA – 如何压缩已高亮显示的碎片化段落?

huangapple go评论133阅读模式
英文:

Microsoft Word VBA - how to condense fragmented paragraphs that are highlighted?

问题

我正在尝试创建一个宏,用于在我的Word文档中查找没有段落完整性的高亮文本并删除换行符以压缩文本。

例如,在下面的图片中,我需要宏将这个文本转换为:

Microsoft Word VBA – 如何压缩已高亮显示的碎片化段落?

我对Word VBA相当新手,所以我尝试编写它。

Sub CondenseZap()

    Dim rngTemp As Range
    
    Set rngTemp = ActiveDocument.Range(Start:=0, End:=0)
    With rngTemp.Find
        .ClearFormatting
        .Highlight = True
        
        ' 下面是你尝试查找和替换换行符的代码
        ' ...
        
    End With

End Sub

这段代码没有执行,并且我不确定如何编写退出条件。我尝试了一下,并成功使程序识别出所有高亮显示的单词,只是不确定如何替换每个单词的换行符。

注意:我不想干扰其他加粗文本之间的换行符(BFS '9不应与"solvency."合并。只有高亮显示的部分应该合并)

如果有人能帮助我组合这个,我会非常感激。

英文:

I'm trying to create a macro to find highlighted text with no paragraph integrity in my Word document and remove the linebreaks to condense the text.

For instance, in the image below, I need the macro to convert this:

Microsoft Word VBA – 如何压缩已高亮显示的碎片化段落?

To this:

Microsoft Word VBA – 如何压缩已高亮显示的碎片化段落?

I'm fairly new to Word VBA, so I tried my hand at making it.

Sub CondenseZap()

'
' CondenseZap Macro
'
'

    Dim rngTemp As Range
 
    Set rngTemp = ActiveDocument.Range(Start:=0, End:=0)
    With rngTemp.Find
     .ClearFormatting
     .Highlight = True
     
     With CondenseRange.Find
            .Text = "^p"
            .Replacement.Text = " "
            .Execute Replace:=wdReplaceAll
        
            .Text = "  "
            .Replacement.Text = " "
            
            While InStr(CondenseRange.Text, "  ")
                .Execute Replace:=wdReplaceAll
            Wend
            
            If CondenseRange.Characters.Item(1).Text = " " And _
            CondenseRange.Paragraphs.Item(1).Range.Start = CondenseRange.Start Then _
            CondenseRange.Characters.Item(1).Delete
        End With
        
     End With
     
    End With
      
End Sub

This code doesn't execute, and I'm not sure how to write the exit conditions. I tested out and got the program to identify all the words with highlighting, I'm just not sure how to replace the linebreak for each at the end.

NOTE: I don't want to mess with the linebreak between the other bolded text (BFS '9 shouldn't be merged with "solvency." Only the highlighted parts)

If someone could help me put this together, I'd really appreciate it.

Cheers.

答案1

得分: 0

您提供的代码有错误,请纠正!比较文本的最简单方法是使用突出显示条件:

Sub CondenseZap()
   
    Dim rngTemp As Range, ur As UndoRecord
 
    ' 这样做时,撤销只需要一步
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord "CondenseZap"
    Set rngTemp = ActiveDocument.Range
    
    With rngTemp.Find
        .ClearFormatting
        .Text = "^p"
        .Forward = True
        .Wrap = wdFindStop
        
        Do While .Execute()
            If rngTemp.Next Is Nothing Then Exit Do
                
            ' 比较文本的最简单方法是使用突出显示条件: 3 = wdTurquoise
            If rngTemp.Previous.HighlightColorIndex = 3 And rngTemp.Next.HighlightColorIndex = 3 Then
                rngTemp.Text = " "
            End If
            
            ' 重置范围
            rngTemp.SetRange rngTemp.End, rngTemp.Document.Range.End
        Loop
        
    End With

    ur.EndCustomRecord
    
End Sub

这符合您的要求吗?

英文:

The code you gave is simply wrong, please correct it!

The easiest way to compare text is to use highlighting conditions:

Sub CondenseZap()
   
    Dim rngTemp As Range, ur As UndoRecord
 
    Rem do this, it only takes one step when undo
    Set ur = Word.Application.UndoRecord
    ur.StartCustomRecord "CondenseZap"
    Set rngTemp = ActiveDocument.Range
    
    With rngTemp.Find
        .ClearFormatting
        .Text = "^p"
        .Forward = True
        .Wrap = wdFindStop
        
        Do While .Execute()
            If rngTemp.Next Is Nothing Then Exit Do
                
            Rem The easiest way to compare text is to use highlighting conditions: 3 = wdTurquoise
            If rngTemp.Previous.HighlightColorIndex = 3 And rngTemp.Next.HighlightColorIndex = 3 Then
                rngTemp.Text = " "
            End If
            
            'reset the range
            rngTemp.SetRange rngTemp.End, rngTemp.Document.Range.End
        Loop
        
    End With

    ur.EndCustomRecord
    
End Sub

Is this what you want?

huangapple
  • 本文由 发表于 2023年5月31日 23:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定