As mentioned in Automating a Style Sheet, Part 1, I recently came across the StyleThat macro, which facilitates copying of terms from a manuscript to a style sheet. It seemed pretty useful but had a few weak points, so I made some adjustments to its code to try to fix things.
A useful feature that I didn’t add, but would have liked to, would alphabetize terms as they are inserted. Integrating a sorting function into the macro could be done, but would slow the process down a bit down. Instead, I created a stand-alone macro that sorts all of the terms in the style sheet at the same time. This allows one to alphabetize the content as often as desired.
This macro (shown below) works by finding content between adjacent headings, or between a heading and the end of the document, and then sorting these entries into alphabetical order. For this to work properly all headings used to organize terms need to use a custom style named Style Sheet Heading. Since it finds headings based on their style name rather than their content, this macro can be used to sort material organized using categories different from those required by the StyleThat macro (for example, it will sort content organized using the headings “people” and “places”). The Style Sheet Heading style can have any appearance you want; the macro only cares about its name.
When sorting, the macro will ignore trailing spaces or line returns, but blank lines within a set of terms will get sorted to the top of the list, so it is best to avoid including these.
Operation of the macro is simple: just make your style sheet the active Word document and run the macro. I have found it handy to run the macro fairly regularly, and recommend assigning a keyboard shortcut to it.
If you find this macro useful, if you have problems with it, or if you have suggestions for improvements, please let me know.
The Code
Sub SortStyleSheet() 'Recommended shortcut: ALT+1 Dim i As Paragraph Dim sortrange As Range flag = 0 'flag value: 0=no heading yet located; 1=heading located; 2=entries to sort located Selection.Collapse Direction:=wdCollapseEnd For Each i In ActiveDocument.Paragraphs If i.Style.NameLocal = "Style Sheet Heading" Then If flag = 2 Then While (Asc(Selection.Characters.Last) = 13) Or (Asc(Selection.Characters.Last) = 32) Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Wend If Len(Trim(Selection.Text)) > 1 Then Selection.Sort SortOrder:=wdSortOrderAscending flag = 1 Else flag = 1 End If Else If flag = 1 Then Set sortrange = i.Range sortrange.Select flag = 2 Else If flag = 2 Then Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend End If End If End If Next While (Asc(Selection.Characters.Last) = 13) Or (Asc(Selection.Characters.Last) = 32) Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Wend If flag = 2 Then If Len(Trim(Selection.Text)) > 1 Then Selection.Sort SortOrder:=wdSortOrderAscending End If End Sub
0 Comments