Posts Tagged ‘Styles’

h1

Word: Custom heading styles cannot be used for caption numbering

March 7, 2022

Here are some things I found out last week when a client called and asked for help with table and figure captioning:

  • You can ONLY use chapter/section numbering in a table/figure etc. caption IF you use Word’s Heading 1 to Heading 9 styles for your headings (and they are set for outline numbering), as these are the ONLY options available to you on the Caption Numbering window of the Caption window
  • If you’ve created your own outline numbered heading style and called it MyCompany Heading 1, you cannot select that style for auto numbering the captions. (A little more info: https://social.technet.microsoft.com/Forums/office/en-US/391594c0-a20c-4676-9592-b8f825b1ce57/change-a-custom-heading-style-in-such-a-way-that-the-caption-tool-recognises-the-heading-so-the?forum=word)
  • If you try to rename the in-built Heading 1 style to MyCompany Heading 1, then when you click OK, Word will change the style name to Heading 1,MyCompany Heading 1 (i.e. it will append your name to the Heading 1 style name)
  • If you right click immediately before the chapter numbering field in a caption and select Toggle Field, you’ll see { STYLEREF 1 \s }, where StyleRef is the field code, ‘1’ represents the heading number (1 = Heading 1), and \s is an undocumented switch code that I think means to reset the sequence after each Heading 1 (based on some information I found here for SEQ field codes: http://www.addbalance.com/usersguide/fields.htm).

So, what can you do? Well, you have a few options…

Option 1: Use Word’s Heading 1 style

The simplest option is to use Word’s Heading 1 style for your top-level headings. Style it according to what you want (font and paragraph settings, numbering, tabs etc., even add your own name to the style name), but don’t set up your own style for heading 1s if you also want to use auto numbered captions. There are other reasons to use Word’s own heading styles too—see this article for 16 reasons why: https://shaunakelly.com/word/numbering/usebuiltinheadingstyles.html

Option 2: Modify the StyleRef field code

Update a week later: This option may NOT work successfully. When I tried it, I’d get one or two right, then I’d do another in a new heading and the first ones wouldn’t work, or I’d get a continuous sequence number for the second part of the caption number. 

This option is feasible if you use your own heading 1 style and only have a few captions. If you have many, it will become very tedious and hard for others to manage if they don’t know what you did. In this method, you’ll change the ‘1’ in the field code to the name of your own heading 1 style (I’ve used MyCompany Heading 1 as the example; your style name will be different):

  1. Right-click immediately before the chapter numbering field in a caption and select Toggle Field Codes. You should see { STYLEREF 1 \s }. If you don’t, then position your cursor immediately in front of the chapter number part of the caption number (the first number) and try again.
  2. Right-click in the field (anywhere inside the { } ), then select Edit Field.
  3. Select your own heading 1 style from the list, then click OK.
  4. The caption number should now reflect your custom heading 1 chapter/section number. If it says Error! No text of specified style in document. then either you don’t have any outline numbers for your custom heading 1 style or you haven’t used that style in your document.
  5. If you right-click on the field again, you should see { STYLEREF “MyCompany Heading 1” \s }, where MyCompany Heading 1 is the name of your custom heading 1 style.
  6. Repeat these steps for EVERY table/figure or other caption you have that uses chapter/section numbering as part of the caption.

Tip: Get one right, then copy/paste it to replace other captions, then update all the fields. I haven’t tried this, but it should work. Test on a couple first, not the several hundred you have—if it works on those, then repeat for the others you have.

Option 3: Use an add-in

In my searching for an answer to this issue, I found a free add-in that’s available from one of the very helpful people on the Word VBA forums: http://jay-freedman.info/custom_style_captions.htm

Notes:

  • I have NOT installed or used this add-in so I can’t comment on its usefulness or not; however, the author of it is someone I trust who knows their stuff.
  • The webpage for this add-in says that it works (has been tested) up to Word 2016. There’s a very good chance that it works with later versions of Word too, but as I haven’t tested it I can’t confirm that.
  • I suspect that this add-in might only work on Word for Windows—I don’t have Word for Mac so I can’t test it on that.

[Links last checked March 2022]

h1

Word: Deleting many unused styles at once

February 23, 2022

Today I went down the rabbit hole of how to delete hundreds of unused styles in a document. Deleting them manually, even using the Organizer (see my blog post on this: https://cybertext.wordpress.com/2010/01/28/word-remove-unwanted-styles-quickly/), is tedious, so I wanted to find if there was an easier way using a macro or similar.

Bottom line: It may be easier to use a macro or one of the other methods (1 to 4) described below if you have a fairly short and not terribly complex document. BUT if you have a large complex document (e.g. with control boxes for document properties, outline numbering, modified settings for inbuilt styles such as heading and TOC styles), you could be making more work for yourself than doing it manually, consider using the macro in Method 5 (added after this post was originally written). 

Test environment: I used a copy of a client’s 440p document, which had appendices, outline numbering, portrait/landscape sections, headers and footers, control boxes for doc properties that were repeated in the headers/footers, automated cross-references, automated caption numbering etc. It also had hundreds of styles, many of which seemed to be unused, that seemed to have come in from other documents or had been created for a specific need—it was these I was trying to get rid of quickly. I printed the list of styles to PDF—this document was 129p long!

My Google searching led me to a couple of sites and forums that seemed to address this issue, a couple of which used macros to achieve the task. One used a version of ‘maggying’ a document (named for the late Maggie Secara, after whom the technique is named), so I tried that first—why use brute force if something simple can achieve what you want?

Method 1: Maggie the document and put it into a new blank document

I found this method here: https://darrengoossens.wordpress.com/2019/04/11/removing-unused-styles-from-a-word-document/. It seemed an ideal solution—quick and simple, with the outcome I wanted. However, it just didn’t work for me. After 45 minutes waiting for the copied text in the 440p document to paste, with ‘not responding’ in the title bar of Word, I finally killed the process. However, I haven’t written off this method as it could be a good solution for smaller documents with many styles. (See Method 4 for some issues I found using this method on a complex document.)

Method 2: Allen Wyatt’s macro

The next thing I tried was a macro from Allen Wyatt, the WordTips guy: https://word.tips.net/T001337_Removing_Unused_Styles.html. From another forum I was aware that the header/footer styles might get removed using his macro, so I was ready to check that. However, I got ‘not responding’ in Word again, and after 40 mins waiting for something to happen, I killed the process again. This may be a solution for a smaller document, but didn’t work for me—I expect a macro to take a few minutes at most.

Method 3: Paul Edstein’s macro

The next macro was one from Paul Edstein (a variation of Allen Wyatt’s DeleteUnusedStyles macro) that I found here: https://www.msofficeforums.com/word-vba/37489-macro-deletes-unused-styles.html. Supposedly, this one didn’t remove header and footer styles, which are always listed as unused even when they are (yes, that’s Word being Word…). This one took about 25 mins to run on my 440p document, and the list of styles seemed to reduce. However, when I checked them, many of the unused styles were still listed and hadn’t been deleted at all. These styles were user-added (not inbuilt styles) and as far as I know, were not linked to other styles, though it’s difficult to tell what’s linked to what.

Method 4: Method 3 + Method 1

As a final test, I took the copy of the document I’d saved from Method 3 and ran Method 1 on it; that is, I ran Paul Edstein’s macro, then used the Maggie method to put the document into a new blank document.

This time the copied text (all 440p) pasted quickly (within a minute) and Word didn’t hang. However, all the unused styles still remaining after running the Method 3 macro were copied across, even though all indications are that they AREN’T used. But the resulting document had further issues that would have to be fixed manually. For example:

  • I lost the content in the doc control boxes for the doc #, doc title etc. in the headers/footers and main body of the document—the control boxes were still there, but the content was gone. These were easy enough to add back in by copy/pasting from the original document, but be aware that this content, if you use it, may disappear.
  • All style formatting for built-in styles such as Caption, TOC 1, TOC 2 etc. and Table of Figure styles was lost. I could easily import them back in using the Organizer. Heading 1, 2, 3 etc. had lost the outline numbering and instead had reverted to a different numbering schema, different fonts, different colours, etc. Again, I used the Organizer to reestablish these.

With these results, I didn’t bother checking any further.

Was this combined method worth it? No, because of the time taken in identifying and then fixing up the issues with the styles.

******

As a final note, when I printed the styles used in the doc (to PDF, NOT paper), the untouched original had 129p of style definitions listed. I fully expected this to be less after deleting all the unused styles using the methods listed here, but instead the PDF of the styles in the modified document (Method 4) ran to 143p!!! This PDF included the styles I’d manually deleted after checking there were unused. This matches the information I discovered as I was researching this, and that’s that the ‘in use’ styles are ANY styles ever used in the document, whether they are still in use or have been deleted.

There has to be an easier way that actually deletes all the unused styles and that doesn’t create even more work or take time that could be used manually deleting them. And Microsoft needs to look at why ‘in use’ styles include ANY styles EVER used in the document, even after they’ve been deleted—‘in use’ to me means ‘being used right now’, not was used 5 years ago on an older template.

Update 25 Feb 2022: One of the commentators on this post shared another macro, which, after some tweaking (because WordPress changes things like plain quote to smart quotes, hyphens/dashes, underscores and totally removes angle brackets, etc.), I got to work on my long document. I’ve added it as Method 5.

Method 5: Simon Jones’ macro

This macro has two parts—first it identifies how many styles are in-built and how many are custom (user-added). Once it’s done that, you are asked to click Yes or No to continue to delete the unused custom styles. In my test document, this part of the macro identified almost 900 (!!!!) custom styles.

Message box telling me that there were 897 custom styles used in the document, and 374 in-built styles

After the macro had run (and it took about 30 minutes to run on the 440p document—an acceptable time, in my opinion), I was left with 38 custom styles that were used in the document. A much more manageable list!

Message after the first part of the macro ran saying there were 38 custom styles remaining in the document

Notes about this macro:

  • If you’re using a version of Word prior to Word 2007, you will have to comment out the lines that have ### in them.
  • Copy/paste this macro—some of it goes off the visible window

 

Sub DeleteUnusedStyles()
 
'Simon Jones, MillStream Designs Ltd, 05/03/2007
'macro originally called DeleteUnusedStylesTake3

Dim objStyle As Word.Style
Dim intBuiltInStyles As Integer
Dim intStyle As Integer
 
'Count the number of built-in styles in this document
 
For Each objStyle In ActiveDocument.Styles
    
    If objStyle.BuiltIn Then
        intBuiltInStyles = intBuiltInStyles + 1
    End If
    
Next objStyle
 
'Show summary and ask permission to continue
 
If MsgBox("There are " & intBuiltInStyles & " built-in styles " & vbCrLf & _
          "and " & ActiveDocument.Styles.Count - intBuiltInStyles & _
          " custom style(s) in this document." & vbCrLf & vbCrLf & _
          "Delete unused custom styles?", vbOKCancel + vbExclamation, _
          "Delete Unused Styles") = vbOK Then
          
    'OK to continue
    'Run through all the styles in the document BACKWARDS -
    'so we only delete styles from the end of the collection
    
    For intStyle = ActiveDocument.Styles.Count To 1 Step -1
        
        If Not ActiveDocument.Styles(intStyle).BuiltIn Then
            'This is a custom style
            'See if it is in use
            
            With ActiveDocument.Content.Find
                .ClearFormatting
                .Style = intStyle
                .Execute Format:=True
                
                If Not .Found Then
                    'Style is not in use
                    'We can delete it
                    
                    With ActiveDocument.Styles(intStyle)
                        'If this style is linked to another we have to unlink them -
                        'otherwise both styles will be deleted which causes problems -
                        'this style may be linked to a built-in style which will cause -
                        'an error if we try to delete it.
                        
                        If .Type <> wdStyleTypeCharacter Then
                            'Character styles can't be linked from, only linked to
                            
                            'The Linked property only exists in Word 2007 and above -
                            'for previous versions, comment out the "If" and "End If" lines below
                            
                            If .Linked Then   '### Word 2007+ only
                                'The LinkStyle property only exists in Word 2002 and above -
                                'for previous versions, comment out the lines below
                                
                                If Not .LinkStyle Is ActiveDocument.Styles(wdStyleNormal) Then '### Word 2002+ only
                                   .LinkStyle = wdStyleNormal '### Word 2002+ only
                                End If       '### Word 2002+ only
                                
                                'LinkStyle is a very badly behaved property and this is the -
                                'closest we can get to unlinking styles short of copying all -
                                'text except the last paragraph mark to a new document.
                            End If           '### Word 2007+ only
                            
                        End If
'
                        'Ignore any error caused by trying to delete
                        'a style whose name begins with a space
                        On Error Resume Next
                        .Delete
                        On Error GoTo 0
                    End With
                
                End If
                
            End With
            
        End If
    
    Next intStyle
    
    'Display summary
    MsgBox "There are " & ActiveDocument.Styles.Count - intBuiltInStyles & _
           " custom style(s) left in this document.", vbInformation, "Unused Styles Deleted"
End If
 
End Sub

 

[Links last checked February 2022]