在 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

相關內容