locked
Applying a set of corporate styles RRS feed

  • Question

  • We have a corporate set of styles defined in a base template. This template is then applied to a number of other templates (technical templates, sales material etc).

    We have modified the base template with updated styles using the same names as the previous base template. These updated styles apply to the other templates nicely. However we have also removed a number of obselete styles.

    The problem we're having is that when we apply the base template to a document/template, the old styles are still there and available for use. Even if they're not used in the document.

    Is there a way of enforcing a specific set of styles that also removes obselete ones? And perhaps chosing the default style to revert to if an obselete style is used in a document?

    Thanks in advance for your help!

    Wednesday, December 5, 2012 11:52 PM

Answers

  • Unless you want to manually process every document, a macro is the only way you can remove the unwanted Styles. FWIW, a macro can also be used to replace a deprecated Style that's in use with a new one (before deleting the old one). Be careful, though, as this can affect the appearance of the document and that might matter in some cases.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Rosscomatic Thursday, December 13, 2012 4:29 AM
    Thursday, December 13, 2012 12:16 AM

All replies

  • You could employ a Document_Open macro in the template to go through the documents and delete the obsolete Styles. However, if those Styles are in use, deleting them will revert whatever used them to the 'Normal' Style. Furthermore, unless the template is stored in a trusted location, users' macro security settings may prevent the macro from running and, conversely, having the macro run every time a document based on the template is opened might introduce an annoying delay before the user can gain access.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, December 6, 2012 7:15 AM
  • Add to Paul,

    If the obsolete styles you mentioned are built-in styles, as far as I know, we cannot delete them, only hide them from the recommended list. That means the users can awalys reach the hiden styles if we choose "All styles" in Style Pane Options --> Select styles to show.


    Max Meng
    TechNet Community Support

    Thursday, December 6, 2012 7:25 AM
  • Hi,

    Just checking in to see how is everything going on?


    Max Meng
    TechNet Community Support

    If you have any feedback on our support, please click here

    • Edited by Max Meng Monday, December 10, 2012 6:38 AM
    Monday, December 10, 2012 6:37 AM
  • Hi guys...

    The styles that we want to remove aren't built-in styles. They're custom ones that we built but are now depricated.

    So a Macro is the only way to go then?

    Regards

    Ross

    Wednesday, December 12, 2012 11:55 PM
  • Unless you want to manually process every document, a macro is the only way you can remove the unwanted Styles. FWIW, a macro can also be used to replace a deprecated Style that's in use with a new one (before deleting the old one). Be careful, though, as this can affect the appearance of the document and that might matter in some cases.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Rosscomatic Thursday, December 13, 2012 4:29 AM
    Thursday, December 13, 2012 12:16 AM
  • Here's a macro to get you started. Simply insert the deprecated Style names into the expression:
    StrLst = "|Style1|Style2|Style3|"
    in the 'UpdateFiles' sub, with the Style names delimited by pipe characters as indicated. The approach taken here obviates the need for a Document_Open macro. Instead, it processes all the files as a once-off exercise. Protected files are not processed, so a report of them is output to the document from which the macro is run.

    The macro has its own browser, so all you should need to do is to point it to the top folder in question. As a bonus, the updated files' original date/time stamps are restored.

    Option Explicit
    Public FSO As Object 'a FileSystemObject
    Public oFolder As Object 'the folder object
    Public oSubFolder As Object 'the subfolders collection
    Public oFiles As Object 'the files object
    Public i As Long, j As Long

    Sub Main()
    ' Minimise screen flickering
    Application.ScreenUpdating = False
    Dim StrFolder As String
    ' Browse for the starting folder
    StrFolder = GetTopFolder
    If StrFolder = "" Then Exit Sub
    ' Search the top-level folder
    Call GetFolder(StrFolder & "\")
    ' Search the subfolders for more files
    Call SearchSubFolders(StrFolder)
    ' Return control of status bar to Word
    Application.StatusBar = ""
    ' Restore screen updating
    Application.ScreenUpdating = True
    MsgBox i & " of " & j & " files updated.", vbOKOnly
    End Sub

    Function GetTopFolder() As String
    GetTopFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetTopFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

    Sub SearchSubFolders(strStartPath As String)
    If FSO Is Nothing Then
      Set FSO = CreateObject("scripting.filesystemobject")
    End If
    Set oFolder = FSO.GetFolder(strStartPath)
    Set oSubFolder = oFolder.subfolders
    For Each oFolder In oSubFolder
      Set oFiles = oFolder.Files
      ' Search the current folder
      Call GetFolder(oFolder.Path & "\")
      ' Call ourself to see if there are subfolders below
      SearchSubFolders oFolder.Path
    Next
    End Sub

    Sub GetFolder(StrFolder As String)
    Dim strFile As String
    strFile = Dir(StrFolder & "*.doc")
    ' Process the files in the folder
    While strFile <> ""
      ' Update the status bar is just to let us know where we are
      Application.StatusBar = StrFolder & strFile
      Call UpdateFiles(StrFolder & strFile)
      strFile = Dir()
    Wend
    End Sub

    Sub UpdateFiles(strDoc As String)
    Dim Doc As Document, StrLst As String, Stl As Style
    Dim oItem As Object, StrDtTm As String
    StrDtTm = FileDateTime(strDoc)
    StrLst = "|Style1|Style2|Style3|"
    ' Open the document
    Set Doc = Documents.Open(strDoc, AddToRecentFiles:=False, ReadOnly:=False, Format:=wdOpenFormatAuto, Visible:=False)
    With Doc
      If .ProtectionType = wdNoProtection Then
        ' Update the document
        For Each Stl In .Styles
          With Stl
            If .BuiltIn = False Then
              If .InUse = False Then
                If InStr(StrLst, "|" & .NameLocal & "|") > 0 Then .Delete
              End If
            End If
          End With
        Next
        ' Update the file counter for changed files
        i = i + 1
      Else
        ' Output a 'protected' file report in the document from which the macro is run.
        ThisDocument.Range.InsertAfter vbCr & strDoc & " protected. Not updated."
      End If
      ' Update the main file counter
      j = j + 1
      .Close SaveChanges:=True
    End With
    ' Let Word do its housekeeping
    DoEvents
    ' Reset the file's Date/Time stamp.
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oItem = FSO.GetFile(strDoc)
    On Error Resume Next
    If IsDate(StrDtTm) Then
      If oItem.DateLastModified <> StrDtTm Then oItem.DateLastModified = StrDtTm
    End If
    Set Doc = Nothing
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Thursday, December 13, 2012 7:28 AM Minor enhancements
    Thursday, December 13, 2012 7:22 AM