Adding a primary topic and it becoming the selected(focused?) topic by VBA

Michael S. shared this question 3 years ago
Discussion Open

Hi,

Using VBA, I would like that when I have selected a topic, I can then run a macro that produces topics at the same level and populates each of the new topics from an array. The original selected topic would contain the first element of the array.

I have managed to get close in that I can build sub-topics from the currently selected topic and populate them.

Here is a portion of the code:

'With MMapp.ActiveDocument

'For counter = 0 To UBound(MyArray)

'Set currentTopic = .Selection.PrimaryTopic.AllSubTopics.Add

'currentTopic.Text=MyArray(counter)

'Next counter

'End With


I then have to delete the original selected topic in order to leave the created sub-topics behind.

I am sure the problem is around this line: 'Set currentTopic = .Selection.PrimaryTopic.AllSubTopics.Add

But I cannot work it out.


Many thanks in advance for any suggestions.

Replies (4)

photo
1

I think you can do that with the databaser linker addin.

And with excel datamapper


https://youtu.be/Ho1PJv6Ih9c

photo
1

Thank you Ary, but that is not what I am looking for.


The command: Set currentTopic = .Selection.PrimaryTopic.AllSubTopics.Add builds a new subtopic, do you know the command to creat a topic at the same level as the current topic?

photo
1

I’ve never got into VBA with MindManager and this may be a completely naive answer, but if there isn’t a command to do what you want, is there any potential simply to delete the unwanted parent topic programmatically, ie, to automate what you’re doing manually now?

photo
1

Hi Alex, your answer is not naive at all, so thank you. I have done that as part of my code. But I am just wondering if there is a way to avoid that and so make my code more efficient.

photo
2

Hello MichaelThere is no method for directly adding a sibling topic, so your code will need to add subtopics to the parent of the current selection. The quick and dirty way would be:

With ActiveDocument.Selection

Set currentTopic = .PrimaryTopic.ParentTopic.AllSubTopics.Add

End With


This will always add new topics at the end of the set of siblings, which may not be what you need if the selected topic is in the middle. You can use the Insert method to move a topic to a new position amongst its siblings.

Set m_Insert = .PrimaryTopic.ParentTopic.AddSubTopic("b1")         .PrimaryTopic.ParentTopic.SubTopics.Insert(m_Insert, IndexOf(.PrimaryTopic) + 1)
As far as I know there is no property for the index of a topic in its parent collection, but it does not take long to find: 

Function IndexOf(ByVal m_Topic As Topic) As Integer

Dim i_1 As Integer

IndexOf = 0

If Not (m_Topic.IsCentralTopic Or m_Topic.IsFloatingTopic) Then

For i_1 = 1 To m_Topic.ParentTopic.AllSubTopics.Count

If m_Topic Is m_Topic.ParentTopic.AllSubTopics(i_1) Then

IndexOf = i_1

Exit For

End If

Next

End If

End Function


Collection indexes are all 1-based in MindManager, not 0-based. Note that the above will fail if the Central Topic is selected (as there is no parent) and will give unexpected results if a floating topic or callout topic are selected. Some extra code to protect against these situations would be good.

photo
1

Function IndexOf(ByVal m_Topic As Topic) As Integer
     Dim i_1 As Integer
     IndexOf = 0
     If Not m_Topic.IsCentralTopic Then
         For i_1 = 1 To m_Topic.ParentTopic.AllSubTopics.Count
             If m_Topic Is m_Topic.ParentTopic.AllSubTopics(i_1) Then
                 IndexOf = i_1
                 Exit For
             End If
         Next
     End If
 End Function

photo
1

Hi Nick, thank you for the confirmation that there is no method for directly adding a sibling topic. That answers my query and stops me from looking for a method that does not exist.

---