0

I wrote a simple code in VBA for MS WORD, in which I want to add dot at the end of each paragraph that has no dot.

The code is as follows:

Function FindParagraph(ByVal doc As Document, ByVal Npara As String) As Paragraph
    Dim para As Paragraph
    
    For Each para In doc.Paragraphs
        If para.Range.ListFormat.ListString = Npara Then
            Set FindParagraph = para
        End If
    Next para
End Function

Sub End_para_with_dot()
    Dim doc As Document
    Dim tb As table
    Dim prange As Range
    Dim srange As Range
    Dim para As Paragraph
    Dim spara As Paragraph
    Dim epara As Paragraph
    Dim txt As String
    
    Set doc = ActiveDocument
    
    Set spara = FindParagraph(doc, "1") 
    Set epara = FindParagraph(doc, "2")
    Set srange = doc.Range(spara.Range.Start, epara.Range.Start) 'sets a specific range of paragraphs in doc
    
    For Each para In srange.Paragraphs
        Set prange = para.Range
        With prange
            If .Style <> "Nagłówek 1" Then
                Debug.Print .Text
                txt = Trim(.Text)
                n = Len(txt)
                last_c = Mid(txt, n - 1, 1)
                If last_c <> "." Then
                    txt = Left(txt, n - 1) & "." & Chr(13)
                    Debug.Print txt
                End If
                .Text = txt '!!!SUPPOSED REASON FOR ERROR!!!
            End If
        End With
    Next para
End Sub

Unfortunately, after I run this code an infinite loop is produced with the first found paragraph being print all the time.

I suppose that it is due to .Text = txt line. Earlier I made a reference to the range object in this statement Set prange = para.Range. But I do not understand why when I want to reassign the .Text property of this object then the infinite loop is produced.

I would be grateful for any tip.

3
  • You can do that very simply via Find/Replace, without the need for a macro. Or you could record the Find/Replace as a macro. Commented Nov 24, 2020 at 9:06
  • Thanks @macropod. I have problem in finding proper signatures of end of line, etc. For example I am using ^v as paragraph character (inserted via app) and it is not searching for the right thing. I do not know where to find the Microsoft list. I heard it may be specific for locale. Am I right? Commented Nov 24, 2020 at 13:58
  • 1
    The way to search paragraph mark - the non-printing character that makes Word break to a new paragraph - is using ^p. The ^v search term looks for a character on the screen that looks like a paragraph mark when non-printing characters are displayed, the literal ¶ Commented Nov 24, 2020 at 15:53

1 Answer 1

1

I'm assuming you don't want to add a . when the paragraph ends with any of !.,:;?

Try a wildcard Find/Replace, where:

Find = ([!\!.,:;\?])(^13)
Replace = \1.\2

Or, as a macro:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "([!\!.,:;\?])(^13)"
    .Replacement.Text = "\1.\2"
    .Format = False
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.