Partager via


Setting a shape’s tool tip and alt text

Here’s another tip that concerns Publisher Shape objects. When you mouse hover over a shape on a publication page, Publisher displays the shape name as a tool tip. You can set the shape name to anything you want, and this becomes the tool tip displayed by Publisher on mouse hover.

The default name of a shape is usually a generic shape type description, followed by a number that denotes the order in which shapes were added to the publication. For example, ‘Picture 12’ is a picture, and was most likely the twelfth shape added to the publication. When you mouse hover over a shape, Publisher displays the default shape name, minus the numbering; it displays ‘Picture’, as opposed to ‘Picture 12’.

To set the name of a shape, just set the Shape.Name property to the string of your choice. If you end the string with a number, Publisher displays that too. For example, if the default name of a shape is ‘Picture 12’, Publisher displays ‘Picture’. But if you overwrite the default name and explicitly set the name to ‘Picture 12’, then Publisher displays ‘Picture 12’. Remember that each individual shape in a grouped shape also has a name which displays as you mouse hover. As far as I can tell, the group shape name only displays when you hover over the rotation tool on the grouped shape. To access the shapes in a grouped shape, use the Shape.GroupItems property.

Shape names must be unique, however; trying to name multiple shapes the same thing generates an error.

The tool tip that Publisher displays on mouse hover shouldn’t be confused with alternative text. Alternative text applies to web pages; it’s the text shown while an image is downloading, or if the user has their browser set to not displays pictures. It is completely separate from a shape’s name. By default, a shape’s alternative text is empty; Publisher does not set the default alternative text equal to the same name.

To demonstrate this, do the following: create a blank web page, and insert several shapes on the page. Hover over them to see Publisher’s default tool tips. Then run the following code, which generates custom names and alternative text for each shape:

Sub AddAltTextToShapes()

Dim shpLoop As Shape

Dim intCount As Integer

intCount = 1

 

With ActiveDocument.Pages

    For Each shpLoop In .Item(1).Shapes

        shpLoop.Name = "MyShape " & intCount

        shpLoop.AlternativeText = "Alt Text For MyShape " & intCount

        intCount = intCount + 1

    Next

End With

End Sub

In the Publisher file, you can now hover over the shapes and Publisher displays the custom shape names. Preview the web page in your browser (File > Web Page Preview). Now set your browser to not display pictures, and refresh the page. (In IE, this options is buried here: Tools > Internet Options > Advanced > Multimedia > Show Pictures.) You should see the custom alternative text for each shape.

Here’s a routine that sets the alternative text of a shape equal to that shape’s name. Just be aware that you can nest grouped shapes, so if you had grouped shapes several levels deep, you’d have to add recursive logic to set the alternative text for those nested shapes as well.

Sub SetShapeNameAsAddAlt()

Dim shpLoop As Shape

Dim shpLoop2 As Shape

 

With ActiveDocument.Pages

    For Each shpLoop In .Item(1).Shapes

        shpLoop.AlternativeText = shpLoop.Name

        If shpLoop.Type = pbGroup Or _

            shpLoop.Type = pbGroupWizard Then

            For Each shpLoop2 In shpLoop.GroupItems

                shpLoop2.AlternativeText = shpLoop2.Name

            Next

        End If

  Next

End With

End Sub