View Single Post
 
Old 09-04-2021, 11:59 AM
Peterson Peterson is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default How is a variant array created to pass properties to a style?

How do I create a variant array to pass multiple properties of different data types to a style?

I recorded a macro that modifies all nine multilevel-list-style levels. It's long/repetitive, I'll need to do the same to other multilevel list styles, so I'm trying to streamline it.

The macro revises 10 properties in each level: some are strings, others are numeric (exact types are in the code). Putting them in a delimited string, splittling (which only works with string arrays), then converting to a variant array doesn't work -- looks like the variant array is an exact copy of the string array.

The attached file also includes the following code, plus some text with the multilevel-list style applied. The macro is shorter than it looks -- I commented the heck out of it because...terrible memory. And it doesn't loop yet because...this forum question.

Code:
Sub SpecTemplate_ConvertMultilevelListStyle_6() ' 09/04/2021

' This macro:
'   1 Converts a string of level properties for a multilevel-list style to an array
'   2 Converts the string array to a variant array
'   3 Applies the level properties to a level style
    
    Dim strStyleName As String
    Dim arr_Str_PropertiesFromString() As String ' Can't use Split function to create variant array, so use a string-
                                                 ' type array as prelim step to getting properties into a variant array
    Dim arr_Var_LevelProperties() As Variant ' Use variant array to receive initial string array and apply properties
    Dim lngIndex As Long ' Array index for copying string array to variant array
    
    ' Populate initial string array:
    arr_Str_PropertiesFromString = Split("""%1""|wdTrailingTab|wdListNumberStyleArabic|0|wdListLevelAlignLeft|0.5|0.5|0|1|""Heading 1""", "|")
    
    ' ReDim variant array to same size as string array:
    ReDim arr_Var_LevelProperties(LBound(arr_Str_PropertiesFromString) To UBound(arr_Str_PropertiesFromString))
       
    ' Put string array into variant array:
    For lngIndex = LBound(arr_Str_PropertiesFromString) To UBound(arr_Str_PropertiesFromString)
        arr_Var_LevelProperties(lngIndex) = arr_Str_PropertiesFromString(lngIndex)
    Next lngIndex
    
    For lngIndex = LBound(arr_Var_LevelProperties) To UBound(arr_Var_LevelProperties)
        Debug.Print arr_Var_LevelProperties(lngIndex)
    Next
        
    ' Update a specific multilevel list style
    strStyleName = "Spec List Style"
       
    With ActiveDocument.Styles("Spec List Style").ListTemplate.ListLevels
                
        With .Item(1)                                                       ' DATA TYPE:
            .NumberFormat = arr_Var_LevelProperties(0)                      ' 8 (String)
            .TrailingCharacter = arr_Var_LevelProperties(1)                 ' 3 (Long Integer)
            .NumberStyle = arr_Var_LevelProperties(2)                       ' 3 (Long Integer)
            .NumberPosition = InchesToPoints(arr_Var_LevelProperties(3))    ' 4 (Single-precision floating-point number)
            .Alignment = arr_Var_LevelProperties(4)                         ' 3 (Long Integer)
            .TextPosition = InchesToPoints(arr_Var_LevelProperties(5))      ' 4 (Single-precision floating-point number)
            .TabPosition = InchesToPoints(arr_Var_LevelProperties(6))       ' 4 (Single-precision floating-point number)
            .ResetOnHigher = arr_Var_LevelProperties(7)                     ' 3 (Long Integer)
            .StartAt = arr_Var_LevelProperties(8)                           ' 3 (Long Integer)
            .LinkedStyle = "Heading 1" ' arr_Var_LevelProperties(9)         ' 8 (String)
        End With
    End With
End Sub
Attached Files
File Type: docm Spec Template_Rebuild_13.docm (39.3 KB, 8 views)
Reply With Quote