Help - macro to insert a return before each capital letter in selected text failing
Hi,
I need help with a macro that will insert a hard return before every capital letter in a selected text range. I do not want to either delete or change the capital letters in any way.
I am copying Medieval poetry from an image file into a Word document using a screen capture and OCR program (ShareX). Unfortunately, the hard returns in the image are not recognized.
So text formatted like the following,
That thou forget not this for nothing;
But look, thou hold it well in thy mind,
For the best thou shalt it find.
For, as the wise man saith and proveth,
becomes a single paragraph rather than formatted poetry when I paste it into a Word Document:
Child, I bid thee on my blessing, That thou forget not this for nothing; But look, thou hold it well in thy mind, For the best thou shalt it find. For, as the wise man saith and proveth,
forcing me to insert hard returns manually
I want to use a VBA macro to insert hard returns in the original text into my Word document
After consulting various Internet Word websites, I wrote a macro based on the following principles:
- Find each of the capitals in the selected text with a FIND statement
- Note:
- Invariably a capital letter starts a new line of verse.
- If there are any exceptions I would manually edit them.
- While the Find code is running insert a hard return before every capital letter.
This is the resulting macro:
Code:
Sub InsertReturnsBeforeCapitalLetters()
'Find all captial letters in selected range and insert a hard return before each one
With Selection.Find
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[A-Z]"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Insert a paragraph return before each capital letter found
Do While Selection.Find.Found
'Do I need 'Selection.InsertBefore' here?
Selection.InsertBreak
Loop
End With
End Sub
Unfortunately it is not reformatting the select text for reasons I cannot determine. The text remains unchanged.
I would be grateful for any help forum members could give me in this regard.
My kind thanks for your help, advice and suggestions.
Sorry! My mistake. your macro works fine
Quote:
Originally Posted by
rollis13
No idea on how you used my suggestion but if you create a new clean file and paste my macro as it is it will work exactly for what it's supposed to do, that is, insert a Return before every capital letter.
Sorry, I made a mistake running your macro. I've run it again and it works great.
Thanks so much for your kind effort and help.
Again, my apologies for not running your macro correctly in the first place.