#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
Quote:
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. |
Tags |
style, vba, word 16 |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Saved style set changing | ljd108 | Word | 1 | 10-21-2014 11:43 AM |