Help needed with a macro code to add a marker icon

prem shared this question 2 days ago
Answered

Hi all,

I'm struggling with the code to programmatic add the below "custom icon 1" to a topic.

Here's the code that I'm using but that is not working.

  ' Access to marker group "Émoticônes"
    On Error Resume Next
    Set MarkerGroup = MonDocActif.MapMarkerGroups.Item("Émoticônes")
    On Error GoTo 0

    'Check if Group Markers  exist
    If MarkerGroup Is Nothing Then
        Exit Sub
    End If

    ' Look for marker "Custom Icon 1"
    Set Marker = Nothing
    On Error Resume Next
    Set Marker = MarkerGroup.MapMarkers.Item("Custom Icon 1")
    On Error GoTo 0

    ' Check if marker exist
    If Marker Is Nothing Then
        Exit Sub
    End If

the below line returns an type mismatch error.

    Set MarkerGroup = MonDocActif.MapMarkerGroups.Item("Émoticônes")

Any idea?

Thanks a lot


a3f6ddf6e0fc145ad5554587bb47fae8

Replies (1)

photo
1

Is this code from a generative AI tool? The parameter for Document.MapMarkerGroups.Item() is an integer for the 1-inclusive index of the marker group, not the group name. The same applies to markers in a marker group. To find a group by name, you will need to iterate over the collection.

photo
1

yes it has been created via ChatGPT...and very helpful so far.. Unfortunately I don't have any developer knowledge

photo
1

@Nick: would you mind helping, with an explicit code example. Please ;-)

photo
1

actually, based on your suggestion I got this working, which return the right MarkerGroup

Set MarkerGroup = MonDocActif.MapMarkerGroups.Item(8) --> this is working

now, I'm struggling with this part:

Set Marker = MarkerGroup.MapMarkers.Item(X), where I've tried with X has an interger or as a string
which return a Method or property not found

any hints, please ?

photo
2

To include extra groups of icons or images or... try Package Folders.

This is a standard MindManager function that you find under Options.

Play around with it and no need for macro's or programming.

photo
2

The method MapMarkerGroup.Item() accepts an integer from 1 to n, where n is the number of markers in the group. It does not accept the marker label.

The following function is extracted from another macro. It asserts a tag marker in a marker group by first finding the group, then searching for the tag by name. If it is not found then it is created. It shows how to iterate over the markers in a group. You could adapt this for icons instead of tags.


Function TagAssert(ByVal s_TagName As String) As MapMarker
    ' Find or create a tag in the group
    Dim m_Tag As MapMarker

    Set TagAssert = Nothing
    s_TagName = Trim(s_TagName)
    If Len(s_TagName) > 0 Then
        For Each m_Tag In TagGroupAssert
            If LCase(m_Tag.Label) = LCase(s_TagName) Then
                Set TagAssert = m_Tag
                Exit For
            End If
        Next
        If TagAssert Is Nothing Then ' first instance is definition
            Set TagAssert = TagGroupAssert.AddTextLabelMarker(s_TagName)
        End If
    End If

End Function

Function TagGroupAssert As MapMarkerGroup
	Dim m_Group As MapMarkerGroup

	If m__TagGroup Is Nothing Then
		Set m__TagGroup = TagGroupFind
		If m__TagGroup Is Nothing Then
			Set m__TagGroup = m__Document.MapMarkerGroups.AddTextLabelMarkerGroup(T_GroupName)
			m__TagGroup.MutuallyExclusive = False
		End If
	End If
	Return m__TagGroup

End Function
Private m__TagGroup As MapMarkerGroup

photo
1

Thanks Nick. I think I'm almost there, but still having 1 major issue: still couldn't find the right code to apply my marker.

I was able to retrieve the category id (Emoticones=8), my custom icon id (custom Icon 1 =8) and the label of my custom icon (="Custom Icon 1")

Any help to tackle the remaining issue would be highly appreciated. I'm totally desperate 😵
Thanks
the following function returns an error at "Topic.AddMarker Marker"

' Function to apply a marker to a topic
Sub ApplyMarkerToTopic(ByVal Topic As Topic, ByVal Marker As MapMarker)
    ' Apply the marker to the topic
     Topic.AddMarker Marker
End Sub
Here's the debug Msgbox
8292e0a958995ba65c59110bb380035f

a7862546916223c2ab96548adb40545c

and here's my complete code:


Sub Main()
    ' Declaration of variables
    Dim MonDocActif As Document
    Dim SujetSelectionne As Topic
    Dim TexteSujet As String
    Dim MarkerGroup As MapMarkerGroup
    Dim Marker As MapMarker
    Dim MarkerIndex As Integer
    Dim MarkerList As String
    Dim GroupList As String
    Dim TempMarker As MapMarker
    Dim i As Integer

    ' Initialize the active document
    Set MonDocActif = ActiveDocument

    ' Check if any topics are selected
    If MonDocActif.Selection.Count = 0 Then
        MsgBox "Debug: No topics selected. MonDocActif.Selection.Count = " & MonDocActif.Selection.Count, vbExclamation, "No topic selected"
        Exit Sub
    End If

    ' 🔹 **Display all marker groups and their index numbers**
    GroupList = "List of marker groups:" & vbCrLf
    For i = 1 To MonDocActif.MapMarkerGroups.Count
        GroupList = GroupList & "Index " & i & " - " & MonDocActif.MapMarkerGroups.Item(i).Name & vbCrLf
    Next
    GroupList = GroupList & vbCrLf & "Total Marker Groups = " & MonDocActif.MapMarkerGroups.Count
    MsgBox GroupList, vbInformation, "Marker Categories (Debug Info)"

    ' 🔹 **Check if the group "Émoticônes" exists, otherwise create it**
    Set MarkerGroup = FindOrCreateMarkerGroup(MonDocActif, "Émoticônes")
    MsgBox "Debug: MarkerGroup assigned. Group Name = " & MarkerGroup.Name & ", Marker Count = " & MarkerGroup.Count, vbInformation, "Debug Info - MarkerGroup"

    ' 🔹 **Display all markers in the selected category**
    MarkerList = "Markers in '" & MarkerGroup.Name & "' :" & vbCrLf
    For i = 1 To MarkerGroup.Count
        Set TempMarker = MarkerGroup.Item(i)
        MarkerList = MarkerList & "Index " & i & " - " & TempMarker.Label & vbCrLf
    Next
    MarkerList = MarkerList & vbCrLf & "Total Markers = " & MarkerGroup.Count
    MsgBox MarkerList, vbInformation, "List of available markers (Debug Info)"

    ' 🔹 **Find the marker "Custom Icon 1" in the group by retrieving its index**
    MarkerIndex = FindMarkerByName(MarkerGroup, "Custom Icon 1")

    ' Check if the marker was found
    If MarkerIndex = -1 Then
        MsgBox "Error: The marker 'Custom Icon 1' is not found.", vbExclamation, "Warning"
        Exit Sub
    Else
        ' Use the index to get the Marker object
        Set Marker = MarkerGroup.Item(MarkerIndex)
        MsgBox "Debug: Marker found. Marker Label = " & Marker.Label, vbInformation, "Debug Info - Marker Found"
    End If

    ' 🔹 **Display the name of the selected marker**
    MsgBox "The selected marker is: " & Marker.Label & vbCrLf & "Debug: Marker index = " & MarkerIndex, vbInformation, "Marker to Add"

    ' 🔹 **Loop through each selected topic**
    For i = 1 To MonDocActif.Selection.Count
        ' Get the selected topic
        Set SujetSelectionne = MonDocActif.Selection.Item(i)
        ' Retrieve the topic text
        TexteSujet = SujetSelectionne.Text

        ' ✅ **Apply the marker "Custom Icon 1" to each selected topic**
        ApplyMarkerToTopic SujetSelectionne, Marker
        MsgBox "Debug: Applied Marker '" & Marker.Label & "' to topic with text: " & TexteSujet, vbInformation, "Debug Info - ApplyMarker"
    Next i

    ' Clean up memory
    Set SujetSelectionne = Nothing
    Set MonDocActif = Nothing
End Sub

' Function to find or create a marker group
Function FindOrCreateMarkerGroup(ByVal Doc As Document, ByVal GroupName As String) As MapMarkerGroup
    Dim MarkerGroup As MapMarkerGroup
    Dim i As Integer

    ' Search for the existing marker group
    For i = 1 To Doc.MapMarkerGroups.Count
        If Doc.MapMarkerGroups.Item(i).Name = GroupName Then
            Set FindOrCreateMarkerGroup = Doc.MapMarkerGroups.Item(i)
            Exit Function
        End If
    Next

    ' If not found, create it
    Set FindOrCreateMarkerGroup = Doc.MapMarkerGroups.AddIconMarkerGroup(GroupName)
End Function

' Function to find a marker by its name and return its index (Integer)
Function FindMarkerByName(ByVal MarkerGroup As MapMarkerGroup, ByVal MarkerName As String) As Integer
    Dim i As Integer

    ' Loop to search for the marker by name
    For i = 1 To MarkerGroup.Count
        If LCase(MarkerGroup.Item(i).Label) = LCase(MarkerName) Then
            FindMarkerByName = i
            MsgBox "Debug: Found Marker at index " & i & vbCrLf & _
                   "Marker Label = " & MarkerGroup.Item(i).Label, vbInformation, "Debug Info - FindMarkerByName"
            Exit Function
        End If
    Next

    ' If no marker is found, return -1
    FindMarkerByName = -1
    MsgBox "Debug: No marker found for '" & MarkerName & "' in group '" & MarkerGroup.Name & "'.", vbExclamation, "Debug Info - FindMarkerByName"
End Function

' Function to apply a marker to a topic
Sub ApplyMarkerToTopic(ByVal Topic As Topic, ByVal Marker As MapMarker)
    ' Apply the marker to the topic

     Topic.AddMarker Marker

    MsgBox "Debug: In ApplyMarkerToTopic - Topic.Text = " & Topic.Text & vbCrLf & _
           "Applied Marker = " & Marker.Label, vbInformation, "Debug Info - ApplyMarkerToTopic"
End Sub

I've also tried something like below but without any success

' Function to apply a custom icon to a topic
Sub ApplyCustomIcon(ByVal m_Topic As Topic, ByVal IconName As String)
    On Error Resume Next

    ' 🔹 **Display available icons before adding**
    Dim IconList As String
    Dim i As Integer
    IconList = "List of available icons:" & vbCrLf

    For i = 1 To m_Topic.Icons.Count
        IconList = IconList & "- " & m_Topic.Icons.Item(i).Name & vbCrLf
    Next

    MsgBox IconList, vbInformation, "Icon Verification"

    ' 🔹 **Add the icon**
    m_Topic.Icons.AddCustomIconFromMap IconName

    On Error GoTo 0

    MsgBox "The icon '" & IconName & "' has been applied to the topic: " & m_Topic.Text, vbInformation, "Icon Applied"
End Sub

photo
Leave a Comment
 
Attach a file