The Story Corner » Discussions


Writing Aid: VBA Sentence Coloring

  • Member
    March 31, 2016

    I made a quick little writing aid and decided to share it here. What it does is cycle through background shade colors for each sentence in a selection. This makes it easier to visually separate your sentences while reviewing. Some uses could be: checking sentence length variation; how you start and end each sentence; or checking sentence structure. I set the default colors to cannibal hippy earth tones, but they're easy to change. There is another Sub to undo the color that works the same way. I recommend not selecting 40+ pages at once; it takes awhile to run. Below are some snap shots:

    BEFORE

    AFTER

    Below is the code with instructions. It is possible to assign these to a hotkey or create a dialog box, but I think that the process to implement that is more difficult for both developer and the user than opening the developer tab and hitting run.

    The code is also attached as a .txt

    -----

    'Open MS Word Developer Tab (To enabe: File > Options > Customize Ribbon > Customize the Ribbon > Main Tabs > Check Developer)
    'Visual Basic > Select current Project > Insert Module > Past the text below


    'Changes the sentences of a selection to alternate between 3 colors as an editing aid.
    'The default colors are hippy earthtones, but you can change the RGB values to your liking.
    'To Use: Select a section of text from your document, click on this Sub, run (F5)
    Sub Colors()

    With ActiveDocument
        Dim color(2) As Long
        'change the values for different colored sentences
        color(0) = RGB(222, 184, 135)
        color(1) = RGB(240, 230, 140)
        color(2) = RGB(167, 202, 95)
        
       For i = 1 To Selection.Sentences.Count
            Selection.Sentences(i).Font.Shading.BackgroundPatternColor = color(j)
            If j = 2 Then
                j = 0
            Else
                j = j + 1
            End If
       Next i
             
       
    End With
    End Sub
    'Removes the color from selected sentences. Highlighting with 'No Color' won't work since
    'the sub uses Shading for more color options.
    'To Use: Select a section of text from your document, click on this Sub, run (F5)
    Sub NoColor()

    With ActiveDocument
            
       For i = 1 To Selection.Sentences.Count
            Selection.Sentences(i).Font.Shading.BackgroundPatternColor = -603914241 'I have no idea why this number means 'no shading'
            
       Next i
             
       
    End With
    End Sub

    -----

  • Member
    March 31, 2016

    Nice little writing aid you posted here, Exuro. :)

    BTW, how do you add that navigation tab on the left side of the screen? Kinda not use to Word.

  • Member
    March 31, 2016

    Thanks! I might make a series of these if anyone has ideas.

    Took a screen shot: