Delete all hyperlinks in one go

Started by Ricky78, March 28, 2025, 12:11:01 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ricky78

Hi, I have many shapes with hyperlinks on a page and would like to delete them all in one go rather than one-by-one. Does anyone have a utility/macro or similar for this available? Thx

Yacine

#1
Sub DeleteAllHyperlinks()
    Dim shp As Visio.Shape
    Dim i As Integer
   
    ' Loop through all top-level shapes on the active page
    For Each shp In ActivePage.Shapes
        ' Delete hyperlinks from last to first to avoid index shifting
        For i = shp.Hyperlinks.Count To 1 Step -1
            shp.Hyperlinks(i).Delete
        Next i
    Next shp
End Sub

Explanation:

Key is to know your object model.

First you record a macro where you do what your future macro shall do.

Sub Macro1()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Insert Hyperlinks")
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).Hyperlinks.ItemU("Row_1").Delete
    Application.EndUndoScope UndoScopeID1, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

A lot of useless stuff in it. Try to identify the key command:

Application.ActiveWindow.Page.Shapes.ItemFromID(1).Hyperlinks.ItemU("Row_1").Delete

 Application.ActiveWindow.Page.Shapes.ItemFromID(1) is the shape.

 .Hyperlinks.ItemU("Row_1").Delete is the actual command. But it refers to a specific row. So you need to figure out how to generalize it and later iterate over all the rows.

Luckily you got a hint in the form of "shape.Hyperlinks", that's a thing you can look for in the manuals: https://learn.microsoft.com/en-us/office/vba/api/visio.shape.hyperlinks

And you read that hyperlinks is a collection. So why not iterate over it with a for loop. in your editor try hyperlinks(i) or hyperlinks.item(i). Use the debug.print function for the inspection. Hey it worked.

Pack your new code in a loop that covers all the shapes. Done.
Yacine

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 172 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1053KB.
Tokens: post-login.
Cache hits: 14: 0.00250s for 26,588 bytes (show)
Cache misses: 3: (show)
Queries used: 15.

[Show Queries]