In a macro, set the position where to add a new topiv

  • 1
  • Question
  • Updated 9 months ago
By manually inserting topics into a tree, some can use the Enter-Key, Insert-Key or Ctrl-Enter-Key, the Shift-Enter-Key to select the position where the new topic should be placed.
So for the
  • Insert-Key or Ctrl-Enter-Key it' a sub-topic(sub - last)
  • Enter-Key this is below the current topic(sibling after current)
  • Shift-Enter-Key it's a topic above the current topic (sibling before current)
How can I do the sibling positioning within a macro?

Just a little of a background:
I am combining some topics out of a selection.
And I 's like to plave the newly created topic just next/below the the last selected item.
Photo of Kurt Ludikovsky

Kurt Ludikovsky

  • 12 Posts
  • 0 Reply Likes

Posted 9 months ago

  • 1
Photo of Nick Duffill

Nick Duffill, Champion

  • 505 Posts
  • 168 Reply Likes
Hello Kurt

There is no method for directly inserting a topic in the middle of a set of sibling topics, but there is a method for moving a topic to a specific place. So you can create the topic first, then move it to the right place.

The following macro creates ten sibling topics, creates a new topic then moves it to index position 6.

Sub Main
    Dim m_Topic As Topic
    Dim m_Doc As Document
    Dim i_Index As Integer

    Set m_Doc = Documents.Add
    For i_Index = 1 To 10
        Set m_Topic = m_Doc.CentralTopic.AddSubTopic(CStr(i_Index))
    Next

    i_Index = 6
    Set m_Topic = m_Doc.CentralTopic.AddSubTopic("New topic")
    m_Doc.CentralTopic.SubTopics.Insert(m_Topic, i_Index)
    MsgBox(CStr(GetTopicIndex(m_Topic)) & ". " & m_Topic.Text)

End Sub

Function GetTopicIndex(ByVal m_Topic As Topic) As Integer
    Dim i_Index As Integer

    GetTopicIndex = 0
    For i_Index = 1 To m_Topic.ParentTopic.AllSubTopics.Count
        If m_Topic.ParentTopic.AllSubTopics.Item(i_Index) Is m_Topic Then
            GetTopicIndex = i_Index
            Exit For
        End If
    Next
End Function
 
To insert a topic in a specific place you will need to calculate the index of the destination. There is no property for the index of a topic amongst its siblings. The function GetTopicIndex() above calculates the index of a topic. Using a value of 0 for the Topics.Insert method will insert at the end of the collection.
Photo of Kurt Ludikovsky

Kurt Ludikovsky

  • 12 Posts
  • 0 Reply Likes
Works like a charm.
THX