How to hide all table styles

2

I have a Word 2010 DOCX file and I'd like to hide all the fancy table styles that do not fit our CI/CD. It does not seem to be possible with the means of the UI, so I need a macro.

I tried the following

Sub Macro1()
    Dim s As Style

    For Each s In ActiveDocument.Styles
        If s.Type = wdStyleTypeTable Then
            If s.NameLocal <> "Table Grid" Then
                Debug.Print (s.NameLocal)
                s.Visibility = False
                s.UnhideWhenUsed = False
                Call s.Delete
            End If
        End If
    Next
End Sub

which should hide and delete all table styles except "Table Grid", but there are still too many styles available.

Too many styles

Thomas Weller

Posted 2016-09-14T07:59:35.057

Reputation: 4 102

I've never seen or even know what this is about. Although I have a long history of programming. VBa is another thing. But from what I can see, logically, the line If s.Type = wdStyleTypeTable Then is the mark where ONLY IF IT'S TRUE will it even do what you ask of it, which means only when that is true will the rest of the code be read by Excel. Hide fancy styles? Have you tried changing the Theme? It seems like an eye sore rather than a problem, maybe? Not sure. – ejbytes – 2016-09-22T09:54:33.187

@ejbytes: yes, I want to do it for table styles only, not for paragraph styles or other styles. It's not so much of a problem if those styles are available. The real problem begins when people start using them because of personal preferences. I'm trying to ensure CI/CD by making it at least harder to use a "fancy" style. – Thomas Weller – 2016-09-22T11:14:35.377

Answers

1

Hoping I've understood the question properly. It's a bit late, but this code, created by starting from Shauna Kelly's example and including a test similar to yours, works pretty well for my Word 2010 environment. The difference appears to be your code has .Visibility = False for the ones you want to hide, but it should be True. I agree that makes no sense, so the relevant line from Word VBA's help is pasted as a comment.

Table Styles in Gallery displays only my custom table:

enter image description here

Sub HideTableStyleButMakeItVisibleWhenUsed()
'Hides all non-XX tables from the Gallery unless user manually finds and applies it.

Dim MyTableStyle As Style

    For Each MyTableStyle In ActiveDocument.Styles

        If MyTableStyle.Type = wdStyleTypeTable Then

            If Left(MyTableStyle.NameLocal, 3) = "XX_" Then 'Remain visible in Gallery
                With MyTableStyle
                    .Visibility = False ' True if the specified style is visible as a recommended style in the Styles gallery and in the Styles task pane. Read/write.
                    .UnhideWhenUsed = True
                End With
            Else 'Hide in Gallery
                With MyTableStyle
                    .Visibility = True
                    .UnhideWhenUsed = True
                End With
            End If

        End If

    Next MyTableStyle

End Sub

Neman

Posted 2016-09-14T07:59:35.057

Reputation: 31

0

Is this what your trying to do?


To Hide Table Style But Make It Visible When Used

Sub Hide_Table_Style()

    With ActiveDocument.Styles(Word.wdStyleTableLightShading)
        .Visibility = True ' Yes, True.
        .UnhideWhenUsed = True
    End With

End Sub

Or simply hide table style

Sub Hide_Table_Style()

    With ActiveDocument.Styles(Word.wdStyleTableLightShading)
        .Visibility = True ' Yes, True.
        .UnhideWhenUsed = False
    End With

End Sub

0m3r

Posted 2016-09-14T07:59:35.057

Reputation: 937

1

No this is not what I'm trying to do. What you describe is the result of a quick google search that brings you to http://shaunakelly.com/word/styles/how-to-hide-table-styles-in-word.html

– Thomas Weller – 2016-09-19T06:23:21.473