Archive for February, 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 macros in Method 5 or Method 6 (both 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

Method 6

I ventured down this rabbit hole again, and this time found a simpler macro which seemed to work without too many issues, though on a long document it will take time to run, so take a break and go for a walk or have lunch or whatever. General advice of doing such a big change on a document still applies—run it on a COPY of the document first, then check that there are no intended consequences before you consider running it on the main document. The macro is from Susan Harkins and is described here (along with less brutal methods of deleting styles): https://www.techrepublic.com/article/delete-unused-styles-using-vba-word/

In case that webpage ever gets deleted, I’ve also posted her macro below. As with the macro in Method 5, copy/paste the macro as it runs off the page. Tip: If your document uses a template other than Normal, run this macro, then reapply your specific template, just in case some unused styles from the template get deleted that you may need later on.

Sub DelUnusedStyles()
'Delete all unused styles, except for built-in styles,
'in the current document.
'You can't delete built-in styles.
' From Susan Harkins 2022, https://www.techrepublic.com/article/delete-unused-styles-using-vba-word/

Dim s As Style
For Each s In ActiveDocument.Styles
    'Only execute With if current s isn't a built-in style.
    If s.BuiltIn = False Then
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Style = s.NameLocal
            .Execute FindText:="", Format:=True
            If .Found = False Then s.Delete
        End With
    End If
Next
End Sub

[Links last checked August 2023]

 

 

h1

Transfer speed affected by where you plug in the USB

February 17, 2022

I needed to transfer some files to a backup external hard disk drive (HDD). I plugged the HDD into a USB-3 hub and did the transfer, but I only got a transfer rate of ~36 MB per second. I knew the HDD had a higher maximum transfer speed than that, so I unplugged it from the USB hub (which was also linked to the back of my PC with about a 1.5 m USB extension cable), and plugged it directly into a USB-3 port on the front of the PC. The transfer speed immediately jumped to ~170 MB per second.

Same USB-3 cable, just a different USB port—one that was directly into the PC without going through a hub or using an extension cable.

Update: Simon commented below that a USB-2 device plugged into a USB-3 hub will limit all ports on the hub to USB-2 as well. Guess what many wireless devices like mice and keyboards are??? USB-2!

h1

Delete specific browsing history in Chrome

February 9, 2022

Over our hot summer months, I’ve been staying inside in the cool doing family tree research and verifying source URLs in my family tree software. I’ve haunted the ancestry.com.au website and done thousands of searches looking for elusive family members and the official sources that verify their dates of birth, death, marriage etc. Today I decided to clear some of my browsing history, specifically for the Ancestry pages. But how to delete those I found? Well, there’s a trick, but it’s not obvious from the Chrome history page, which by default shows only 150 search results at a time, and seems to only allow you to click one checkbox at a time to select those to delete. Clicking one checkbox at a time was NOT a viable option for potentially thousands of pages.

Here’s how to bulk delete items from Chrome’s history:

  1. In Chrome, press Ctrl+h to open the history page.
  2. In the search box, type part of the URL and press Enter (in my case, I typed ancestry.com.au).
  3. If you’ve been to that particular domain a lot, you’ll get a list of just 150 results even if there are many more stored by Chrome.
  4. Use the scrollbar to scroll down the list, then keep on scrolling. What this does is expands the 150 results to 300, 900, 1200, 1800 results, etc. (i.e. multiples of 150 or 300 results).
  5. When you stop scrolling and click back into the Chrome window, the number of results will be listed at the top of the results.
  6. Press Ctrl+a. This selects ALL the checkboxes for the number of results listed.
  7. Press Delete to delete them all. Confirm the deletion.
  8. Repeat these steps if you still have more, or do another search if you want to delete the browsing history for another domain.
  9. Close the History tab when you’ve finished.