View Single Post
 
Old 03-14-2023, 03:50 AM
Nirnim Nirnim is offline Windows 11 Office 2016
Novice
 
Join Date: Mar 2023
Posts: 2
Nirnim is on a distinguished road
Question Delete every style in the document that isn't saved in the template

Hello!
As the title implies, I would like to know if there's a way of creating a Macro that checks all the styles that are currently in the active document against all the styles that are saved in the template associated to the document.
I was thinking of something like this (pseudocode-ish):

Code:
Private Sub FixStyles()
    Dim stlStyle As Style
    Dim arrTemplateStyles() As Style ' This array would contain all the styles saved in the template
    ' Maybe there's a built-in collection that has these values and makes this variable unnecessary
    
    ' Iterate through all the styles in the current document
    For Each stlStyle In ActiveDocument.Styles
        ' If the style is not in the list of styles saved in the attached template
        If Not SearchInArray(stlStyle.NameLocal, arrTemplateStyles) Then
            ' Delete said style and go on with the next one
            Delete stlStyle
        End If
    Next stlTemplateStyle
End Sub

Private Function SearchInArray(strName As String, varArray As Variant) As Boolean
    ' Function used to look for a specific item in an array
    Dim blnFound As Boolean
    Dim varItem As Variant
    blnFound = False
    
    For Each varItem In varArray
        If varItem.NameLocal = strName Then
            blnFound = True
            Exit For
        End If
    Next varItem
    SearchInArray = blnEncontrado
End Function
I'm no expert in VBA, Word or anything like that, so any help or tips are welcome!
Reply With Quote