Site icon bVisual

Adding the Sixth Legend Icon in Visio 2010

In my previous two articles, I described how to have 6 icons in a Data Graphic Icon Set, and provided some VBA code to make your own icons sets from existing Visio shapes or images. One thing that I overlooked was the Insert Legend command in Visio 2010, because it does not add the sixth icon automatically, so this blog describes how to add it. As a side effect, the code also enables the legend labels of icon and CBV (Color By Value) items to be edited.



When you use the Insert Legend command, it examines the Data Graphics used on the active page, to create legends for each Icon Set or Color By Value settings that it finds. Closer examination of the legend shapes, shows that an instance of the Icon Set master shape is added for each of the icons found.

So, my method for having a sixth icon as the default if a match is not found present a problem, because it is not displayed automatically.

However, it is quite simple to add the sixth one. Select the icon above the one that is missing, then enter CTL+D to duplicate the shape, and notice that it will be added below it.

So, now there are two icons that look the same, and are both labelled the same.

So, I decided to write a small macro to provide a simple way to display the default icon, and to change the text.

Firstly, the macro needs to recognise if the selected shape is actually a legend shape item for an icon set. This can be tested by checking that the cell User.msvDGLegendShapeType = “IconItem”

The label text is stored in the User.msvDGLegendText cell.

If the legend item is for an icon set, then the sub-shape has the cell User.msvCalloutType with the value “Icon Set” :

The displayed icon is controlled by the value in the User.msvCalloutIconNumber cell.

So, I wrote a macro, ChangeLegendTextOrIcon , that makes these changes simple:

Public Sub ChangeLegendTextOrIcon()
Dim shpLegendItem As Visio.Shape
Dim shpIconSet As Visio.Shape
Dim txt As String
Dim sNumber As String
Dim iNumber As Integer

    'Abort if no shape or too many shapes selected
    If Not ActiveWindow.Selection.Count = 1 Then
        Exit Sub
    End If
    
    'Abort if selected shape is not a legend item
    If ActiveWindow.Selection.PrimaryItem.CellExists("User.msvDGLegendShapeType", Visio.visExistsAnywhere) = 0 Then
        Exit Sub
    Else
        Set shpLegendItem = ActiveWindow.Selection.PrimaryItem
    End If
    
    'Prompt to change the label text
    txt = InputBox("Enter the desired label text", "Change Legend Text", _
        shpLegendItem.Cells("User.msvDGLegendText").ResultStr(""))
    shpLegendItem.Cells("User.msvDGLegendText").FormulaU = "=""" & txt & """"
    
    'Prompt to change the icon number (not for CBVItem)
    If shpLegendItem.Cells("User.msvDGLegendShapeType").ResultStr("") = "IconItem" Then
        Set shpIconSet = shpLegendItem.shapes(1)
        sNumber = InputBox("Enter the icon number ( 0 to 4 normally, or anything else for the default) ", _
            "Change Legend Icon", Int(shpIconSet.Cells("User.msvCalloutIconNumber").ResultIU))
        If IsNumeric(sNumber) Then
            iNumber = Int(sNumber)
            shpIconSet.Cells("User.msvCalloutIconNumber").FormulaU = "=" & iNumber
        Else
            MsgBox "You must enter a number", vbInformation
        End If
    End If
    
End Sub

Then enter the desired icon number. For example, enter 5 to display the default icon:

The legend will now be changed as desired:

Similarly, you can use the macro to change the label of a CBV legend item. In this example, the header is not displayed on one line because the width of the column is controlled by the widest item. So, I selected one item and ran the macro:

I then added some spaces after the text:

This forced the whole column to be widened, thus providing enough width for the header text:

I have added the code to to the stencil from the previous blog – bVisualIconSetMaker.vss :

https://1drv.ms/u/s!AoRdXTjKEAo1ii8gdbp4qu3qF8eK
Exit mobile version