Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2021, 09:45 PM
stky stky is offline Need to do this for more than 20 styles is any option is there. help me on this Windows 10 Need to do this for more than 20 styles is any option is there. help me on this Office 2013
Advanced Beginner
Need to do this for more than 20 styles is any option is there. help me on this
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default Need to do this for more than 20 styles is any option is there. help me on this

Sub test_style_exists()

Dim test_style As String
On Error Resume Next
test_style = "Quota"
MsgBox "Style " & test_style & " exists in " & ActiveDocument.Name & "?" & vbCr & _
style_exists(ActiveDocument, test_style)


If Not MyStyle Is Nothing Then
Set MyStyle = Application.ActiveDocument.Styles.Add(test_style, wdStyleTypeParagraph)
End If

End Sub

Function style_exists(test_document As Word.Document, style_name As String) As Boolean



style_exists = False
On Error Resume Next
style_exists = test_document.Styles(style_name).NameLocal = style_name

End Function
Reply With Quote
  #2  
Old 08-02-2021, 11:55 PM
gmayor's Avatar
gmayor gmayor is offline Need to do this for more than 20 styles is any option is there. help me on this Windows 10 Need to do this for more than 20 styles is any option is there. help me on this Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

How about. Change the style names as required

Code:
Option Explicit

Sub AddStylesFromList()
Dim MyStyle As Style
Dim bExists As Boolean
Dim i As Integer
Dim vStyle(19) As Variant
    vStyle(0) = "Stylename1"
    vStyle(1) = "Stylename2"
    vStyle(2) = "Stylename3"
    vStyle(3) = "Stylename4"
    vStyle(4) = "Stylename5"
    vStyle(5) = "Stylename6"
    vStyle(6) = "Stylename7"
    vStyle(7) = "Stylename8"
    vStyle(8) = "Stylename9"
    vStyle(9) = "Stylename10"
    vStyle(10) = "Stylename11"
    vStyle(11) = "Stylename12"
    vStyle(12) = "Stylename13"
    vStyle(13) = "Stylename14"
    vStyle(14) = "Stylename15"
    vStyle(15) = "Stylename16"
    vStyle(16) = "Stylename17"
    vStyle(17) = "Stylename18"
    vStyle(18) = "Stylename19"
    vStyle(19) = "Stylename20"

    On Error Resume Next
    For i = 0 To 19
        bExists = style_exists(ActiveDocument, CStr(vStyle(i)))
        If bExists = False Then Application.ActiveDocument.Styles.Add CStr(vStyle(i)), wdStyleTypeParagraph
        DoEvents
    Next i
End Sub

Function style_exists(test_document As Word.Document, style_name As String) As Boolean
    style_exists = False
    On Error Resume Next
    style_exists = test_document.Styles(style_name).NameLocal = style_name
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-03-2021, 12:44 AM
stky stky is offline Need to do this for more than 20 styles is any option is there. help me on this Windows 10 Need to do this for more than 20 styles is any option is there. help me on this Office 2013
Advanced Beginner
Need to do this for more than 20 styles is any option is there. help me on this
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default can we add

can we add font name in that.

(e.g.) Times New Roman, Calibri

if we create same as for characters style

can we add the attributes in that

(e.g.) bold, italic, superscript, subscript.
Reply With Quote
  #4  
Old 08-03-2021, 01:05 AM
gmayor's Avatar
gmayor gmayor is offline Need to do this for more than 20 styles is any option is there. help me on this Windows 10 Need to do this for more than 20 styles is any option is there. help me on this Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can include any style parameters e.g. as follows

Code:
Option Explicit

Sub AddStylesFromList()
Dim MyStyle As Style
Dim bExists As Boolean
Dim i As Integer
Dim vStyle(19) As Variant
    vStyle(0) = "Stylename1"
    vStyle(1) = "Stylename2"
    vStyle(2) = "Stylename3"
    vStyle(3) = "Stylename4"
    vStyle(4) = "Stylename5"
    vStyle(5) = "Stylename6"
    vStyle(6) = "Stylename7"
    vStyle(7) = "Stylename8"
    vStyle(8) = "Stylename9"
    vStyle(9) = "Stylename10"
    vStyle(10) = "Stylename11"
    vStyle(11) = "Stylename12"
    vStyle(12) = "Stylename13"
    vStyle(13) = "Stylename14"
    vStyle(14) = "Stylename15"
    vStyle(15) = "Stylename16"
    vStyle(16) = "Stylename17"
    vStyle(17) = "Stylename18"
    vStyle(18) = "Stylename19"
    vStyle(19) = "Stylename20"

    On Error Resume Next
    For i = 0 To 19
        bExists = style_exists(ActiveDocument, CStr(vStyle(i)))
        If bExists = False Then
            Set MyStyle = Application.ActiveDocument.Styles.Add(CStr(vStyle(i)), wdStyleTypeParagraph)
            With MyStyle
                Select Case i
                    Case 0
                        .Font.Name = "Calibri"
                        .Font.Size = 12
                    Case 1
                        .Font.Name = "Times New Roman"
                        .Font.Bold = True
                        .Font.Size = 16
                    Case 2
                        .Font.Name = "Times New Roman"
                        .Font.Italic = True
                        .Font.Size = 14
                    Case 3
                        .Font.Name = "Times New Roman"
                        .Font.Superscript = True
                        .Font.Size = 12
                        'etc
                End Select
            End With
        End If
        DoEvents
    Next i
End Sub

Function style_exists(test_document As Word.Document, style_name As String) As Boolean
    style_exists = False
    On Error Resume Next
    style_exists = test_document.Styles(style_name).NameLocal = style_name
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 08-03-2021, 03:57 AM
stky stky is offline Need to do this for more than 20 styles is any option is there. help me on this Windows 10 Need to do this for more than 20 styles is any option is there. help me on this Office 2013
Advanced Beginner
Need to do this for more than 20 styles is any option is there. help me on this
 
Join Date: Apr 2021
Posts: 30
stky is on a distinguished road
Default Thanks, one more think

while i find and replace its only finding and replace in document body, I need to find the in footnote and endnote too.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to do this for more than 20 styles is any option is there. help me on this Creating buttons for individual paragraph styles on a ribbon tab - not using Quick Styles T7Training Word 11 12-22-2019 12:16 PM
Does a new set of styles in a template overwrite or remove the existing set of styles in a document? dianahbr Word 6 03-27-2018 11:12 PM
Two much blank space between names of styles in the Styles Pane PereCasanellas Word 0 10-06-2017 03:47 AM
Question about spacing between multi-level bullet styles (and other styles) SDwriter Word 0 09-26-2017 09:39 AM
Need to do this for more than 20 styles is any option is there. help me on this Styles: Only this document option jthomas666 Word 1 06-27-2016 12:35 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:24 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