Results 1 to 8 of 8

Thread: Help - macro to insert a return before each capital letter in selected text failing

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jan 2021
    Posts
    13
    Location

    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:


    1. Find each of the capitals in the selected text with a FIND statement
    2. Note:
      1. Invariably a capital letter starts a new line of verse.
      2. If there are any exceptions I would manually edit them.

    3. While the Find code is running insert a hard return before every capital letter.


    This is the resulting macro:

    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.
    Last edited by Aussiebear; 11-15-2022 at 12:35 PM. Reason: Added code tags to supplied code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •