Calling ComObject methods contained in a array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Bachelar
Posts: 28
Joined: 26 Aug 2020, 12:30

Calling ComObject methods contained in a array

Post by Bachelar » 26 May 2022, 04:53

TLDR;
I've put together a working script to insert a custom multilevel list to a currently opened Word document – via ComObj. I've also got a function that returns what is needed (that is all commands), in a form of array (I'm sure if that's the problem and if it's a good way to do it). I don't know how to „call” the content of the array. Another question – is there a better/efficient way to that?

Code: Select all

InsertList()
{
    if WinActive("ahk_class OpusApp")
    {
        oWord := ComObjActive("Word.Application")
        lst := oWord.Application.ListGalleries(wdOutlineNumberGallery := 3).ListTemplates(1)

        roman := wdListNumberStyleUppercaseRoman := 1
        arab := wdListNumberStyleArabic := 0
        letterupper := wdListNumberStyleUppercaseLetter := 3
        letterlower := wdListNumberStyleLowercaseLetter := 4
        stylenone := wdListNumberStyleNone := 255
        alignright := wdListLevelAlignRight := 2
        alignleft := wdListLevelAlignLeft := 0
        aligncenter := wdListLevelAlignCenter := 1

        trailingtab := wdTrailingTab := 0
        tabpos := wdUndefined := 9999999
        start := 1
        res := 0
        inch := 28.35

        trailingtab := wdTrailingTab := 0
        tabpos := wdUndefined := 9999999
        start := 1

        1level := lst.ListLevels(1)
        2level := lst.ListLevels(2)
        3level := lst.ListLevels(3)
        4level := lst.ListLevels(4)
        5level := lst.ListLevels(5)
        6level := lst.ListLevels(6)
        7level := lst.ListLevels(7)
        8level := lst.ListLevels(8)
        9level := lst.ListLevels(9)

        1level.NumberFormat := "%1."
        2level.NumberFormat := "§%2."
        3level.NumberFormat := "%3."
        4level.NumberFormat := "%4)"
        5level.NumberFormat := "%5."
        6level.NumberFormat := "%6)"
        7level.NumberFormat := "⟹"
        8level.NumberFormat := "⋙"
        9level.NumberFormat := "⤷"

        1level.TrailingCharacter := 2level.TrailingCharacter := 3level.TrailingCharacter := 4level.TrailingCharacter := 5level.TrailingCharacter := 6level.TrailingCharacter := 7level.TrailingCharacter := 8level.TrailingCharacter := 9level.TrailingCharacter := trailingtab

        1level.NumberStyle := roman
        2level.NumberStyle := arab
        3level.NumberStyle := arab
        4level.NumberStyle := arab
        5level.NumberStyle := letterupper
        6level.NumberStyle := letterlower
        7level.NumberStyle := stylenone
        8level.NumberStyle := stylenone
        9level.NumberStyle := stylenone

        1level.NumberPosition := 1 * inch
        1level.TextPosition := 1.5 * inch

        2level.NumberPosition := 2 * inch
        2level.TextPosition := 3 * inch

        3level.NumberPosition := 3.5 * inch
        3level.TextPosition := 4.5 * inch

        4level.NumberPosition := 5 * inch
        4level.TextPosition := 5.5 * inch

        5level.NumberPosition := 6 * inch
        5level.TextPosition := 7 * inch

        6level.NumberPosition := 7.5 * inch
        6level.TextPosition := 8.5 * inch

        7level.NumberPosition := 8.8 * inch
        7level.TextPosition := 9.8 * inch

        8level.NumberPosition := 10.1 * inch
        8level.TextPosition := 11.1 * inch

        9level.NumberPosition := 11.3 * inch
        9level.TextPosition := 12.3 * inch

        1level.Alignment := alignright
        2level.Alignment := alignright
        3level.Alignment := alignright
        4level.Alignment := alignright
        5level.Alignment := alignright
        6level.Alignment := alignright
        7level.Alignment := aligncenter
        8level.Alignment := aligncenter
        9level.Alignment := aligncenter

        1level.TabPosition := 2level.TabPosition := 3level.TabPosition := 4level.TabPosition := 5level.TabPosition := 6level.TabPosition := 7level.TabPosition := 8level.TabPosition := 9level.TabPosition := tabpos

        1level.ResetOnHigher := res := 0
        2level.ResetOnHigher := res =+ 1
        3level.ResetOnHigher := res =+ 1
        4level.ResetOnHigher := res =+ 1
        5level.ResetOnHigher := res =+ 1
        6level.ResetOnHigher := res =+ 1
        7level.ResetOnHigher := res =+ 1
        8level.ResetOnHigher := res =+ 1
        9level.ResetOnHigher := res =+ 1

        1level.StartAt := 2level.StartAt := 3level.StartAt := 4level.StartAt := 5level.StartAt := 6level.StartAt := 7level.StartAt := 8level.StartAt := 9level.StartAt := 1

        lst.Name := ""

        oWord.Selection.Range.ListFormat.ApplyListTemplateWithLevel(ListTemplate := lst, ContinuePreviousList := False, ApplyTo := wdListApplyToWholeList := 0, DefaultListBehavior := wdWord10ListBehavior := 2)

        oWord.ActiveDocument.Styles(WdBuiltinStyle := wdStyleListParagraph := -180).NoSpaceBetweenParagraphsOfSameStyle := False

        oWord.Selection.Paragraphs.SpaceAfter := 6
    }
    Return
}
Now I'm looking for a way to shorten it, as it quite long – quite unnecessarily in my opinion. I made an effort and this is what I came up with (biga's library for managing arrays was used):

Code: Select all

LevelsContent()
{
    A := new biga()

    lst := "oWord.Application.ListGalleries(wdOutlineNumberGallery := 3).ListTemplates(1)"

    roman := wdListNumberStyleUppercaseRoman := 1
    arab := wdListNumberStyleArabic := 0
    letterupper := wdListNumberStyleUppercaseLetter := 3
    letterlower := wdListNumberStyleLowercaseLetter := 4
    stylenone := wdListNumberStyleNone := 255
    alignright := wdListLevelAlignRight := 2

    trailingtab := wdTrailingTab := 0
    tabpos := wdUndefined := 9999999
    start := 1
    res := 0
    inch := 28.35

    numberformats := ["%1.", "§%2.", "%3.", "%4)", "%5.", "%6)", "⟹", "⋙", "⤷"]

    numberstyles := [roman, arab, arab, arab, letterupper, letterlower, stylenone, stylenone, stylenone]

    arr := []
    arr2 := []
    arr3 := []
    arr4 := []
    arr5 := []
    arr6 := []
    arr7 := []
    arr8 := []
    arr9 := []

    for n, numberformat in numberformats
    {
        level := lst . ".ListLevels" . "(" . n . ")"
        arr.Push(level . ".NumberFormat := " . """" . numberformat . """")
    }

    for n, numberstyle in numberstyles
    {
        level := lst . ".ListLevels" . "(" . n . ")"

        ps := (start + 1 * (n - 1))
        numpos := Format("{:0.3f}", (ps * inch))
        textpos := Format("{:0.3f}", ((ps + 0.5) * inch))

        arr2.Push(level . ".NumberStyle := " . numberstyle)
        arr3.Push(level . ".NumberPosition := " . numpos)
        arr4.Push(level . ".TextPosition := " . textpos)
        arr5.Push(level . ".TrailingCharacter := " . trailingtab)
        arr6.Push(level . ".Alignment := " . alignright)
        arr7.Push(level . ".TabPosition := " . tabpos)
        arr8.Push(level . ".ResetOnHigher := 0")
        arr9.Push(level . ".StartAt := 1")
    }
    arr := A.zip(arr, arr2, arr3, arr4, arr5, arr6, arr7, arr8, arr9)

    return arr
}
It basically returns what is needed, in a form of array (I'm sure if that's the problem and if it's a good way to do it). Now the problem arises, because I don't know how to "call" (i.e. use them, so the code has any effect) these commands contained in the array.

Return to “Ask for Help (v1)”