[API] MindManager API - How to Add Text While Preserving Formatting

krsto e. shared this question 13 days ago
Answered

Hello MindManager Community,

I'm developing a macro that adds references to selected topics (what I call a "reference popularizer"). The macro works by appending formatted references like "[RefName]" to the end of selected topics. It handles spacing intelligently based on the current text:

- For empty topics, it just adds the reference

- If a topic ends with "]", it adds a space before the new reference

- If a topic already ends with a space, it adds the reference without additional spacing

- Otherwise, it adds a space followed by the reference

The Problem: When I use Topic.Text to modify topic text, it completely removes all existing formatting (colors, underlining, etc.). This creates a frustrating experience for users who carefully formatted their topics.

What I've Tried:

1. Using Topic.Title.InsertText with calculated position - This didn't insert at the expected position

2. Using Topic.Title.TextRTF to get RTF text and then appending to it - This didn't add anything

3. Using Topic.Title.InsertTextRTF - This resulted in an "Unable to execute a command" error

4. Using Topic.Title.GetDisplayText(mmDisplayTextAll) to get text, then adding reference - This still lost formatting

Here's a simplified version of my current code:

' Get current text

currentText = Topic.Text



' Add reference with proper spacing

If currentText = "" Then

Topic.Text = newRef

ElseIf Right(currentText, 1) = "]" Then

Topic.Text = currentText & " " & newRef

ElseIf Right(currentText, 1) = " " Then

Topic.Text = currentText & newRef

Else

Topic.Text = currentText & " " & newRef

End If

My Question: Is there a way to add text to the end of a topic while preserving the existing text formatting? Is there any approach using the MindManager API that allows for appending text without destroying formatting attributes?

I've explored several methods from the API documentation (Title object, GetDisplayText, TextRTF) but haven't found a reliable solution. Any guidance would be greatly appreciated!

Thank you in advance,

Replies (2)

photo
1

ps i have tried 10x times /w claude 3.5 sonnet, and 5+ /w claude 3.7 sonnet + thinking.


those are state of the art LLMs.


i have even already share the api documents to it as much as i can.


thank you.

photo
2

This will be difficult without parsing the RTF. RTF itself is complex and RTF libraries are not easy to find. The method Topic.Text extracts the plain text without formatting. Some alternatives are:

  • Parse the topic XML, modify it and write it back. You may be able to append plain text to the PlainText attribute without destroying existing formatting. If you want to insert text you will need to decode the formatting syntax and adjust the formatting ranges, e.g. '<ap:FontRange Bold="true" From="3" To="6"/>' and so on. Be aware that writing back badly formed XML risks locking yourself out from the map.
  • Use a Tag instead of modifying the topic text.

photo
1

thank you very much.

i am glad we have you on this forum~

photo
1

"Use a Tag instead of modifying the topic text."

OK, may be in my mm to md macro i'll make it able to turn tag which are specific formatted reference names append to the topic text then. thanks

photo
1

Subject: Thank You for the Support!

Hello MindManager Community,

I want to express my gratitude for the support on my "reference popularizer" macro project. Your insights have been invaluable, especially Nick D.'s suggestions.

I determined to brutally try different offsets, and it turns out that using an offset of 1 solved the problem of inserting references while preserving formatting!

Thank you all for being such a supportive community!

Best,
KE

demo: 1st time at 999 with red color, 2nd time add aaa with underline, 3rd time add xxx with Bold.

8378769305883a3f89e5897d69e2e5f9

i'll paste the code below. thank you.

git and github haven't been automated in my work flow.


thanks

photo
1

Option Explicit

Sub Main
    ' Get reference name from user
    Dim refName As String
    refName = InputBox("Enter reference name:", "Add Reference")

    ' Exit if cancelled or empty
    If refName = "" Then Exit Sub

    ' Get selection
    Dim sel As Selection
    Set sel = ActiveDocument.Selection

    ' Check if anything is selected
    If sel.Count = 0 Then
        MsgBox "Please select at least one topic.", vbExclamation
        Exit Sub
    End If

    ' Format reference with brackets
    Dim newRef As String
    newRef = "[" & refName & "]"

    ' Process each selected topic
    Dim i As Integer
    Dim Topic As Topic
    Dim currentText As String
    Dim baseInsertPos As Integer
    Dim finalInsertPos As Integer

    For i = 1 To sel.Count
        If TypeOf sel.Item(i) Is Topic Then
            Set Topic = sel.Item(i)
            
            ' Get current topic text with formatting preserved
            currentText = Topic.Title.GetDisplayText(15) ' 15 = mmDisplayTextAll
            
            ' Find base insert position (end of text)
            baseInsertPos = Len(currentText)
            
            ' Automatically set final insert position with an offset of 1
            finalInsertPos = baseInsertPos + 1
            
            ' Add reference with proper spacing based on current text
            If currentText = "" Then
                ' If topic is empty, just add the reference
                Topic.Title.InsertText 0, newRef
            ElseIf Right(currentText, 1) = "]" Then
                ' If topic ends with ']', add space then reference
                Topic.Title.InsertText finalInsertPos, " " & newRef
            ElseIf Right(currentText, 1) = " " Then
                ' If topic ends with space, just add reference
                Topic.Title.InsertText finalInsertPos, newRef
            Else
                ' Otherwise add space then reference
                Topic.Title.InsertText finalInsertPos, " " & newRef
            End If
        End If
    Next i
End Sub


as said, this macro append reference "xxx" as [xxx] to an existing topic text, now with original format preserved, as it's using the insert text method.


thanks

photo
1

bug: if the original text have any formatting, the inserted text will be the same formatting. seems no good way to solve this. sigh. have to use with care.


2c99da82e4620c6fdf35403ddbe24e4e

photo
Leave a Comment
 
Attach a file