Insert paragraph before a capital letter - word VBA - 6 lines of code

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
126
Something is missing and I can't figure it out. VBA editor highlights error at this line: Mid(MyString, Counter-1, 1).Paragraphs.Add
and I don't know how could I achieve what I need.

Basically I have to loop through every paragraph and examine the first character of it. For example there will be a pharagraph:

'the show must Go on

And I need to insert a new paragraph before "G", so:

'the show must
Go on

What am I missing?

VBA Code:
For Each para In ActiveDocument.Paragraphs

    If para.Range.Font.Size = 12 Then
 
                Txt = para.Range.Text

                If Left(Txt, 1) = "'" Then

                         For Counter = 1 To Len(Txt)
         

                               If Mid(MyString, Counter, 1) = UCase(Mid(MyString, Counter, 1)) Then

                               'SOMETHING IS WRONG HERE
                                Mid(MyString, Counter-1, 1).Paragraphs.Add

                                End If


                         Next

                End If
 
               
 
 
    End If

    Next
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

RayFrye

Board Regular
Joined
Jan 31, 2005
Messages
81
Office Version
365, 2019
Try using chr(13) fr the vbCRLF add.
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,445
Even if you were to get your code working, it is extremely inefficient. Try:
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
  .Font.Size = 12
  .Execute FindText:="([!^13A-Z])([A-Z])", ReplaceWith:="\1^p\2", MatchWildcards:=True, Format:=True, Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
 

RayFrye

Board Regular
Joined
Jan 31, 2005
Messages
81
Office Version
365, 2019
OK, Mr. Paul (retired moderator). You are on top of your game.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,565
Messages
5,487,595
Members
407,605
Latest member
PACULA

This Week's Hot Topics

Top