none
Word macro to highlight lines of varying length RRS feed

  • Question

  • Can a macro be created to highlight text by line, eg. for headings of different lengths? I tried recording a macro but it has a fixed number of characters. So if the heading is short it will highlight text in the next line. Is this possible?
    Tuesday, December 24, 2013 12:37 PM

Answers

  • The problem is identifying which paragraphs you want marked, against which ostensibly similar paragraphs you don't, without access to the document. It would be possible to drop in a prompt for each change, but with a large document that would be slow progress.

    If the document is broken into sections, as you appear to be indicating, you can change the macro to work only on the section the cursor is in as follows

    Sub HighlightShortParas()
    Dim oPara As Paragraph
        For Each oPara In Selection.Sections(1).Range.Paragraphs
            If Len(oPara.Range) < 50 Then
                oPara.Range.Style = "Heading 1"        'Optional
                oPara.Range.HighlightColorIndex = wdYellow
            End If
        Next oPara
    End Sub

    or you could loop through all the sections and process only those required.


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by DavdW Sunday, December 29, 2013 6:03 PM
    Saturday, December 28, 2013 7:28 AM

All replies

  • There are no 'lines' as such in a Word document. The displayed lines are formed by text flow and manual line breaks (Shift+Enter) and paragraph breaks (Enter). I assume you must mean paragraphs. You can certainly highlight paragraphs, but you will have to define what constitutes 'short'.

    Can we take it that you have not used styles to format the document? Had you done so you could search for the heading styles or simply reformat the heading style as required. It is always good practice to use styles when creating documents. However the following should work. It assumes a short line has less than 50 characters and it optionally applies a Heading style. Remove that line if not required. You can change the 50 to whatever length you require.

    Sub HighlightShortParas()
    Dim oPara As Paragraph
        For Each oPara In ActiveDocument.Paragraphs
            If Len(oPara.Range) < 50 Then
                oPara.Range.Style = "Heading 1"        'Optional
                oPara.Range.HighlightColorIndex = wdYellow
            End If
        Next oPara
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    Wednesday, December 25, 2013 6:18 AM
  • Hi Graham

    Thank you very much for your answer and Merry Xmas to you, I never expected a reply on Christmas Day! You are right I have not used style to format the document. The sub procedure works, but it selects every paragraph and that includes the content section where I included hyperlinks. I only it to select headings where there is a paragraph of text immediately underneath the headings.

    Thank you

    David Willis

    Friday, December 27, 2013 11:21 AM
  • The problem is identifying which paragraphs you want marked, against which ostensibly similar paragraphs you don't, without access to the document. It would be possible to drop in a prompt for each change, but with a large document that would be slow progress.

    If the document is broken into sections, as you appear to be indicating, you can change the macro to work only on the section the cursor is in as follows

    Sub HighlightShortParas()
    Dim oPara As Paragraph
        For Each oPara In Selection.Sections(1).Range.Paragraphs
            If Len(oPara.Range) < 50 Then
                oPara.Range.Style = "Heading 1"        'Optional
                oPara.Range.HighlightColorIndex = wdYellow
            End If
        Next oPara
    End Sub

    or you could loop through all the sections and process only those required.


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by DavdW Sunday, December 29, 2013 6:03 PM
    Saturday, December 28, 2013 7:28 AM