Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2022, 03:07 PM
andremadar andremadar is offline Copying and then hiding all but a few "approved" styles into current document Windows 11 Copying and then hiding all but a few "approved" styles into current document Office 2021
Novice
Copying and then hiding all but a few "approved" styles into current document
 
Join Date: Nov 2022
Location: United States
Posts: 1
andremadar is on a distinguished road
Question Copying and then hiding all but a few "approved" styles into current document

Hi! I've been tasked with creating some "official" styles our small law firm (24 attorneys) could use in a variety of documents. I'm self-taught with VBA so please forgive if some of my code looks like pure evil. I sure could use some additional brainpower!
  1. I have the 15 styles already created and stowed in a global template. The template downloads to Word's \STARTUP folder at user login.
  2. The user can run a macro in the global template that uses
    Code:
    Application.OrganizerCopy
    to download the styles into the current document. The macro that calls a small Function for each of the 15 styles is working, although a bit slow. Each style's name begins with the "_" character.



  3. After this fires, I'm enabling the little Style Pane with
    Code:
    Application.CommandBars("Styles").Position = msoBarRight
  4. Here's the problem: I'd like the Style Pane to hide all styles EXCEPT the official ones. But the macro recorder doesn't seem to capture much that goes on inside that window (e.g., marking each style as "Recommended" and so on), and googling hasn't turned up much.

Am I on the right track or am I doing things the hard way as usual...?

Code:
Sub DOCXStylesDownload2()

Dim str_Source As String
Dim str_Destination As String

str_Source = "\\Server\Setup\OfficeTemplates\Automation.dotm"
str_Destination = ActiveDocument.Path & "\" & ActiveDocument.Name

On Error GoTo ErrorHandler

Application.ScreenUpdating = False

Call DOCXStyleCopy2(str_Source, str_Destination, "_Body")
Call DOCXStyleCopy2(str_Source, str_Destination, "_BracketedNo")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Claim")
Call DOCXStyleCopy2(str_Source, str_Destination, "_ClaimBody")
Call DOCXStyleCopy2(str_Source, str_Destination, "_DocTitle")
Call DOCXStyleCopy2(str_Source, str_Destination, "_EquationBody")
Call DOCXStyleCopy2(str_Source, str_Destination, "_EquationNo")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Section")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Section+Break")
Call DOCXStyleCopy2(str_Source, str_Destination, "_SectionSub")
Call DOCXStyleCopy2(str_Source, str_Destination, "_SectionSub+Break")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Step")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Table")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Footer")
Call DOCXStyleCopy2(str_Source, str_Destination, "_Closing")

Application.ScreenUpdating = True

Exit Sub

ErrorHandler:
    MsgBox "Error"
    
End Sub
Function DOCXStyleCopy2(Source, Destination, StyleName As String)

On Error GoTo ErrorHandler

Application.OrganizerCopy Source:=Source, Destination:=Destination, Object:=wdOrganizerObjectStyles, Name:=StyleName

Exit Function

ErrorHandler:
    MsgBox "Error"
    
End Function
Reply With Quote
  #2  
Old 11-17-2022, 09:54 PM
Peterson Peterson is offline Copying and then hiding all but a few "approved" styles into current document Windows 10 Copying and then hiding all but a few "approved" styles into current document Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Try this:
Code:
Sub DOCXStylesDownload2()
' Copies styles from a template to the active document, then hides
' all styles that do not begin with an underscore
 
    Dim str_Source As String
    Dim str_Destination As String

    ''str_Source = "\\Server\Setup\OfficeTemplates\Automation.dotm"
    str_Destination = ActiveDocument.FullName
    
    On Error GoTo ErrorHandler
    
    Application.ScreenUpdating = False
    
    ' Pass source and destination paths to a function to copy styles:
    Call DOCXStyleCopy2(str_Source, str_Destination)
    ' Hide styles that don't begin with an underscore:
    Call DOCXHideStyles
    
    Application.ScreenUpdating = True
    
    Exit Sub

ErrorHandler:
    MsgBox "Error - StylesDownload2"

 End Sub


 Function DOCXStyleCopy2(strSource As String, strDestination As String)
' Loops through all styles in a document/template and copies them to a document
' if they begin with an underscore

    Dim myStyle As Style
    Dim objSourceDoc As Document
    
    On Error GoTo ErrorHandler
    
    ' Open the source document/template:
    Set objSourceDoc = Documents.Open(strSource, ReadOnly:=True)
    ' Loop through all the styles in the document/template:
    For Each myStyle In objSourceDoc.Styles
        ' If a style begins with an underscore:
        If InStr(myStyle.NameLocal, "_") = 1 Then
            '...then copy it to the source file:
            Application.OrganizerCopy Source:= _
            strSource, Destination:= _
            strDestination, Name:=myStyle.NameLocal, Object _
            :=wdOrganizerObjectStyles
        End If
    Next myStyle
    objSourceDoc.Close
    
Exit Function

ErrorHandler:
    MsgBox "Error StyleCopy2"

 End Function


 Function DOCXHideStyles()
' Hides all styles that do not begin with an underscore

    Dim myStyle As Style
    
    On Error GoTo ErrorHandler
    
    For Each myStyle In ActiveDocument.Styles
        ' If the style name doesn't begin with _, then hide it:
        If InStr(myStyle.NameLocal, "_") <> 1 Then
            ' Set visibility in the Styles pane:
            ' NOTE: The VBA setting to HIDE a style is ".Visibility = True" (thanks, Microsoft...):
            myStyle.Visibility = True
        End If
    Next myStyle

Exit Function

ErrorHandler:
    MsgBox "Error DOCXHideStyles"

End Function
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
I clicked "this document" in VBA thinking it was a menu button, it said "saving", how can I reverse messgchr Word VBA 2 07-20-2020 06:52 PM
Copying and then hiding all but a few &quot;approved&quot; styles into current document When applying styles word is automatically reverting to "strong" style partway through my document Victoria S Word 3 11-17-2015 12:51 PM
Creating an "Approved" command button donnac1107 Word VBA 10 09-04-2014 07:45 PM
Copying and then hiding all but a few &quot;approved&quot; styles into current document Macro to print "Current Page" in Word 2010 Jshopping Word VBA 10 06-08-2012 01:15 AM
Copying and then hiding all but a few &quot;approved&quot; styles into current document Citations Appear in "Current List", but not in the document itself jp_igit Word 3 03-30-2012 03:20 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:44 PM.


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