Editing strings that are followed by a punctuation mark in Microsoft Word

0

I have a Microsoft Word document with two sections of text. The first is in paragraph form, the second is that paragraph form broken down into segments and placed in a table without punctuation. Is there a function that I can use so that:

  • If: In the punctuated text, a section is followed by a punctuation mark,
  • Then: In the unpunctuated text within the table, the punctuation is added.

If this is not possible in Microsoft Word then are there other suggested tools for accomplishing this goal?

Here is a screenshot taken in Microsoft Word to illustrate my issue.

Here is an image illustrating task

bpb

Posted 2018-08-17T17:20:22.907

Reputation: 11

Question was closed 2018-08-29T14:38:10.840

2By section do you mean a Word Section? By punctuation mark do you men each paragraph ending in a period? Do you have a screenshot that you can include with your question? It's so much more helpful to see what you are trying to describe. – Rich Michaels – 2018-08-17T17:38:10.873

Hi Rich, I added an image. – bpb – 2018-08-18T18:02:58.973

1@bpb What—if anything—have you done so far in an attempt to solve this issue? This site is not a script coding service. – JakeGould – 2018-08-20T18:24:11.550

Answers

0

You can add missing punctuation to paragraphs in tables cells with the following code. As for the conditions upon which this would get executed, you will have to take a look at my comment and provide more information.

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

Based on your additional questions placed as comments, here are supplements to my original answer:

For a resource on Creating or Running a Macro use this Microsoft Support link. https://support.office.com/en-us/article/create-or-run-a-macro-c6b99036-905c-49a6-818a-dfb98b7c3c9c

As for your other question of adapting the above code based on your new supplied information, this is how to modify it.

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

Rich Michaels

Posted 2018-08-17T17:20:22.907

Reputation: 2 009

Two questions: (1) How do I input a code like this into a MS document? and (2) How would you alter this code so that it would be able to accomplish my task as clarified by the image in my question? – bpb – 2018-08-19T19:27:22.173

@bpb Sounds like you are stringing the community along to do coding for you. The reality is this answer provides an answer to the question you have provided. You need to know how to use macros and modify them to adapt them to your new case. And in all honesty, questions like this that are vague are a textbook example about why nobody should just code a solution like this. The question is vague and this community is not a script coding service. – JakeGould – 2018-08-20T18:23:30.037

Thanks Rich, I will look through those resources and will use your example to help better understand the issue. And thank you Jake for clarifying the use of the forum, I'll be sure to keep your points in mind in the future. – bpb – 2018-08-20T22:01:42.570