Resetting a hyperlink

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15357
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Resetting a hyperlink

Post by ChrisGreaves »

I have had some success using this on Word documents containing hyperlinks.
From time to time I note that editing the hyperlink code (r/c, Edit) is not as effective as preserving the address, fixing the link (Ctrl-Shift-F9), and then rebuilding the link (Ctrl-K).
I submit the code knowing that within 15 minutes six different loungers will have streamlined my code to the point where it is no longer recognizable from the original.
I claim, therefore, to be the initiator of some of the best code in the forum :artist: .

Code: Select all

Sub ResetHyperlink()
    If Selection.Hyperlinks.Count = 1 Then
        Dim rng As Range
        Set rng = Selection.Hyperlinks(1).Range
        Dim strText As String
        strText = rng.Hyperlinks(1).TextToDisplay
        Dim strLink As String
        strLink = rng.Hyperlinks(1).Address
        rng.Fields(1).Unlink
        ActiveDocument.Hyperlinks.Add Anchor:=rng, Address:=strLink, SubAddress:="", ScreenTip:="", TextToDisplay:=strText
    Else
        MsgBox "Please select all or part of a single hyperlink."
    End If
End Sub
You can't develop a solution to a problem until you have defined the problem

User avatar
HansV
Administrator
Posts: 78008
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Resetting a hyperlink

Post by HansV »

In my Word 2007, your macro either crashes Word, or I get an error message "Insufficient memory or disk space"... :sad:
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15357
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Resetting a hyperlink

Post by ChrisGreaves »

HansV wrote:In my Word 2007, your macro either crashes Word, or I get an error message "Insufficient memory or disk space"... :sad:
Thanks Hans.
Does it tell you which line (of the code) causes the crash?
(later) thanks Hans.
Last edited by ChrisGreaves on 06 Mar 2010, 11:41, edited 1 time in total.
You can't develop a solution to a problem until you have defined the problem

User avatar
HansV
Administrator
Posts: 78008
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Resetting a hyperlink

Post by HansV »

Yes, the offending line is the one that adds the hyperlink again:

ActiveDocument.Hyperlinks.Add ...
Best wishes,
Hans

William
StarLounger
Posts: 79
Joined: 08 Feb 2010, 21:48
Location: Wellington, New Zealand

Re: Resetting a hyperlink

Post by William »

The problem with Word 2007 has been documented elsewhere - with a few suggested solutions. See:

http://www.eggheadcafe.com/software/asp ... -insu.aspx" onclick="window.open(this.href);return false;

http://www.eggheadcafe.com/software/asp ... -wont.aspx" onclick="window.open(this.href);return false;

http://www.generation-nt.com/us/answer/ ... 24152.html" onclick="window.open(this.href);return false;

I don't have Word 2007 here, so I haven't been able to test any of these solutions.

User avatar
HansV
Administrator
Posts: 78008
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Resetting a hyperlink

Post by HansV »

Thanks, William, that does solve the problem! :thumbup:
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78008
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Resetting a hyperlink

Post by HansV »

Chris,

Here is a variant :pun: of your code that works in Word 2007 too, with the following extra features:
- If the selection contains more than one hyperlink, all of them will be recreated.
- Subaddresses and screentips will be preserved, if present, instead of being reset to empty strings.

Code: Select all

Sub ResetHyperlinks()
  Dim lnk As Hyperlink
  Dim rng As Range
  Dim varText As Variant
  Dim strAddress As String
  Dim strSubAddress As String
  Dim strScreenTip As String
  
  For Each lnk In Selection.Hyperlinks
    Set rng = lnk.Range
    varText = lnk.TextToDisplay
    strAddress = lnk.Address
    strSubAddress = lnk.SubAddress
    strScreenTip = lnk.ScreenTip
    rng.Fields(1).Unlink
    ActiveDocument.Hyperlinks.Add _
      Anchor:=rng, _
      Address:=strAddress, _
      SubAddress:=strSubAddress, _
      ScreenTip:=strScreenTip, _
      TextToDisplay:=varText
  Next lnk
End Sub
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15357
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Resetting a hyperlink

Post by ChrisGreaves »

HansV wrote:Here is a variant of your code
Thanks Hans. (I just noticed that my earlier reply got lost, probably by me ...)
Your version works in Word 2000, so I'm guessing that a change was made after 2000/2003 into 2007 that changed the type from String to Variant.
How odd.
You can't develop a solution to a problem until you have defined the problem