在 Microsoft Word 中编辑后跟标点符号的字符串

在 Microsoft Word 中编辑后跟标点符号的字符串

我有一个 Microsoft Word 文档,其中包含两段文本。第一段是段落形式,第二段是将该段落形式分解为多个部分并放在没有标点符号的表格中。我可以使用什么函数来实现:

  • 如果:在标点文本中,一个部分后面跟着一个标点符号,
  • 然后:表格内未标点的文字,添加标点。

如果 Microsoft Word 无法实现这一点,那么是否有其他建议的工具可以实现此目标?

这是在 Microsoft Word 中截取的屏幕截图,用于说明我的问题。

这是一张说明任务的图片

答案1

您可以使用以下代码向表格单元格中的段落添加缺失的标点符号。至于执行此操作的条件,您必须查看我的评论并提供更多信息。

Sub AddPunction()
Dim para As Paragraph, tbl As Table, tRow As Row
Dim tCell As Cell, cRng As Range, pRng As Range
For Each tbl In ActiveDocument.Tables
    For Each tRow In tbl.rows
        For Each tCell In tRow.Cells
            Set cRng = tCell.Range
            cRng.MoveEnd wdCharacter, -1
            If cRng.Paragraphs.Count > 0 Then
                For Each para In cRng.Paragraphs
                    Set pRng = para.Range
                    If Asc(pRng.Characters.Last) = 13 Then
                        pRng.MoveEnd wdCharacter, -1
                    End If
                    If Not Asc(pRng.Characters.Last) = 46 Then
                        pRng.Text = pRng.Text & "."
                    End If
                Next para
            End If
        Next tCell
    Next tRow
Next tbl
End Sub

根据您在评论中提出的其他问题,以下是我原始答案的补充:

有关创建或运行宏的资源,请使用此 Microsoft 支持链接。 https://support.office.com/en-us/article/create-or-run-a-macro-c6b99036-905c-49a6-818a-dfb98b7c3c9c

至于您根据新提供的信息调整上述代码的另一个问题,这是修改方法。

Sub TableLookBackAddPunctuation()
Dim para As Paragraph, tbl As Table, tRow As Row
Dim tCell As Cell, cRng As Range, pRng As Range
Dim rng As word.Range
For Each tbl In ActiveDocument.Tables
    For Each tRow In tbl.rows
        Set cRng = tRow.Cells(1).Range
        cRng.MoveEnd wdCharacter, -1
        If cRng.Paragraphs.Count > 0 Then
            For Each para In cRng.Paragraphs
                Set pRng = para.Range
                If Asc(pRng.Characters.Last) = 13 Then
                    pRng.MoveEnd wdCharacter, -1
                End If
                Select Case Asc(pRng.Characters.Last)
                    Case 33, 34, 35, 36, 37, 38, 39, 40, _
                            41, 42, 43, 44, 45, 46, 63
                        'do nothing, punctuation already set
                    Case Else
                        Set rng = pRng
                        rng.Collapse wdCollapseStart
                        With rng.Find
                            .ClearFormatting
                            .Format = False
                            .Forward = False
                            .MatchCase = True
                            .Text = pRng.Text
                            .Wrap = wdFindStop
                            .Execute
                            If .found Then
                                rng.MoveEnd wdCharacter, 1
                                Select Case Asc(rng.Characters.Last)
                                    Case 33, 34, 35, 36, 37, 38, 39, 40, _
                                            41, 42, 43, 44, 45, 46, 63
                                        pRng.Text = pRng.Text & rng.Characters.Last
                                    Case Else
                                        'do nothing, there's no punctuation
                                End Select
                            End If
                        End With
                End Select
            Next para
        End If
    Next tRow
Next tbl
End Sub

相关内容