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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

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,657
Messages
5,488,123
Members
407,625
Latest member
Alanacran

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top