Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2023, 03:50 AM
Nirnim Nirnim is offline Delete every style in the document that isn't saved in the template Windows 11 Delete every style in the document that isn't saved in the template Office 2016
Novice
Delete every style in the document that isn't saved in the template
 
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
  #2  
Old 03-14-2023, 07:26 AM
gmaxey gmaxey is offline Delete every style in the document that isn't saved in the template Windows 10 Delete every style in the document that isn't saved in the template Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Perhaps:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oStyle As Style, oStyleValidate As Style
Dim oDocValidate As Document
Dim oTmp As Template
  Set oTmp = ActiveDocument.AttachedTemplate
  Set oDocValidate = oTmp.OpenAsDocument
  Set oStyle = ActiveDocument.Styles("Style1")
  For Each oStyle In ActiveDocument.Styles
    On Error Resume Next
    Set oStyleValidate = oDocValidate.Styles(oStyle.NameLocal)
    If Err.Number <> 0 Then
      oStyle.Delete
      Err.Clear
    End If
  Next oStyle
  oDocValidate.Close wdDoNotSaveChanges
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 03-14-2023, 08:02 AM
Nirnim Nirnim is offline Delete every style in the document that isn't saved in the template Windows 11 Delete every style in the document that isn't saved in the template Office 2016
Novice
Delete every style in the document that isn't saved in the template
 
Join Date: Mar 2023
Posts: 2
Nirnim is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Perhaps:

Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oStyle As Style, oStyleValidate As Style
Dim oDocValidate As Document
Dim oTmp As Template
  Set oTmp = ActiveDocument.AttachedTemplate
  Set oDocValidate = oTmp.OpenAsDocument
  Set oStyle = ActiveDocument.Styles("Style1")
  For Each oStyle In ActiveDocument.Styles
    On Error Resume Next
    Set oStyleValidate = oDocValidate.Styles(oStyle.NameLocal)
    If Err.Number <> 0 Then
      oStyle.Delete
      Err.Clear
    End If
  Next oStyle
  oDocValidate.Close wdDoNotSaveChanges
lbl_Exit:
  Exit Sub
End Sub
Hi Greg!
I ran your code, but it didn't have the desired effect. I'm sure there's no problem with your code, but rather with where those styles are saved.
I assumed they were saved in the Active Document, but after debugging your Macro, all the values checked by the code match up correctly with the ones saved on the template. The thing is, I can see the undesired styles in the Style window, even if the code somehow ignores them.
I know this has turned into a loaded question, but is there a way to check (or "ask" the program) where a certain style is saved by giving it its name or something similar? Maybe that way I'd be able to modify the Macro you provided to work as intended.
In the (hopefully) attached screenshot you can see all the "garbage" styles I mean, which don't get picked up by the Macro code.
(Sadly, I can't send a sample document for legal reasons, not even after using a text scrambler, like I saw suggested on some other posts, so I understand if this question is impossible to solve as is)
Thank you for your time!
deleteMe.png

Last edited by Nirnim; 03-14-2023 at 08:11 AM. Reason: Added a screenshot for further reference.
Reply With Quote
Reply

Tags
style, vba, word 16

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete every style in the document that isn't saved in the template USB disconnected while document was live; saved document now corrupt. How do I retrieve document? MaxB Word 2 07-01-2019 04:42 PM
My style keeps changing, can't get saved style to apply Meenie50 Word 7 07-20-2017 03:47 PM
Keyboard shortcut or QAT button for a saved paragraph style? VeazyT Outlook 0 05-15-2017 01:38 PM
How to make a fillable word template but not see what is not req'd to be filled in saved as document sjohnstone@edgeautomation Word 1 05-02-2017 10:05 AM
Delete every style in the document that isn't saved in the template Saved style set changing ljd108 Word 1 10-21-2014 11:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:28 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft