How to Insert a hyperlink in Powerpoint 2007

So you think you know how to insert a hyperlink? Well, think again. Microsoft has changed the internal code of Powerpoint 12. The old way of inserting hyperlinks won't work anymore.

Here was my delimma. I needed to be able to programmatically insert a hyperlink into a slide when the user clicked my ribbon (menu) item. My old code looked like this

VB Code:
  1. Sub test1()
  2. With Application.ActiveWindow.Selection.TextRange.ActionSettings _
  3. (ppMouseClick).Hyperlink
  4.  
  5. .Address = "C:\Documents and Settings\chris.dunsford\My Documents\test.doc"
  6. .SubAddress = ""
  7. .ScreenTip = ""
  8. .TextToDisplay = "test"
  9. End With
  10. End Sub


So then I try running the code from Powerpoint help and the hyperlink seems to be inserted but no text will appear. Here is the code from Powerpoint help:


VB Code:
  1. With ActivePresentation.Slides(1).Shapes(1) _
  2. .ActionSettings(ppMouseClick)
  3. .Action = ppActionHyperlink
  4. .Hyperlink.Address = "http://www.microsoft.com/"
  5. End With


The code that finally works is:

VB Code:
  1. Sub AddHyperlinkToSelection()
  2.  
  3.     Dim tr As TextRange
  4.  
  5.     Set tr = ActiveWindow.Selection.TextRange.InsertAfter("Hyperlink")
  6.  
  7.     tr.ActionSettings(ppMouseClick).Action = ppActionHyperlink
  8.  
  9.     tr.ActionSettings(ppMouseClick).Hyperlink.Address = "c:\test.doc"
  10.  
  11. End Sub