Share via

Delete hyperlinks based on a style - VBA Script doesn't work?

Anonymous
2012-02-20T01:20:19+00:00

Hi all,

I need to run a script across a document that removes hyperlinks. However I only want it to remove hyperlinks from text with a certain style applied.

I also want to change the style applied to that text. I thought this would work but it doesn't seem to do anything?

Sub DelHyperlink()

Dim hl As Hyperlink

For Each hl In ActiveDocument.Hyperlinks

If hl.Range.Style = "H4 Small Blue Hyperlink WT" Then

hl.Range.Style = "H4 Smll WT"

ActiveDocument.Hyperlinks(1).Delete

End If

Next

End Sub

Any ideas? Many thanks. HJ

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-02-21T00:38:27+00:00

There are several considerations, which actually depend on how your document is formatted:

  • The best approach is to apply character styles (that is, "H4 Smll WT" has to be a character style, not a paragraph style) to avoid modification of the surrounding text in the paragraph. If you were, for instance, to apply "Heading 1" to a selected hyperlink, the whole paragraph would change to Heading 1.
  • Once the macro selects the complete hyperlink within a paragraph, it looses sight of what the hyperlink's character style is, and instead reports the paragraph style. My previous example worked because the only thing in each paragraph was a hyperlink, and you could not tell that the whole paragraph was being modified to the new style. To avoid this, we need to place the cursor within the hyperlink. Only then should we ask what the style is.

In the following version:

  1. For all hyperlinks, select each one
  2. Instead of asking what its style is as before (which would report the paragraph style), move the cursor back (to deselect) and forth (to enter the hyperlink).
  3. Now ask what the style is (the style under the cursor, which will report the character style's name, if the is one, or the paragraph style otherwise).
  4. If the name matches (in this case, I used the default style name "Hyperlink," which is a character style, but you should use your existing character style), do the rest of the steps:
  5. Select once again the hyperlink
  6. Apply the proper character style (here, called "Red")
  7. Delete the hyperlink.

You can do a step-by-step execution with **** Control Command i to follow along. Let me know how it works.

Sub ReplaceHyperlinkStyle()

For i = ActiveDocument.Hyperlinks.Count To 1 Step -1

ActiveDocument.Hyperlinks(i).Range.Select

Selection.MoveLeft UNIT:=wdCharacter, Count:=1

Selection.MoveRight UNIT:=wdCharacter, Count:=1

If Selection.Range.Style = "Hyperlink" Then

ActiveDocument.Hyperlinks(i).Range.Select

ActiveDocument.Hyperlinks(i).Range.Style = "Red"

ActiveDocument.Hyperlinks(i).Delete

End If

Next

End Sub

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2012-02-20T02:41:31+00:00

For text such as "http://www.microsoft.com" use this:

If ActiveDocument.Hyperlinks(i).Range.text like "*//www.*" then ...

For texts such as "<www.microsoft.com>" use this:

If ActiveDocument.Hyperlinks(i).Range.text like "<www.*" then...

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2012-02-20T21:25:02+00:00

    Thanks for your help Daniel : )

    The script to not delete the links whose text starts with "www."  works really well (I realised the "<" isn't part of the link):

    Sub DeleteHyperlinks2()

    For I = ActiveDocument.Hyperlinks.Count To 1 Step -1

    If ActiveDocument.Hyperlinks(I).Range.Text Like "www.*" Then

    'Do nothing

    Else

    ActiveDocument.Hyperlinks(I).Delete

    End If

    Next

    End Sub

    The other script to delete hyperlinks based on styles is not working for me:

    Sub DeleteHyperlinks()

    For I = ActiveDocument.Hyperlinks.Count To 1 Step -1

    If ActiveDocument.Hyperlinks(I).Range.Style = "H4 Small Blue Hyperlink WT" Then

    ActiveDocument.Hyperlinks(I).Range.Style = "H4 Smll WT"

    ActiveDocument.Hyperlinks(I).Delete

    End If

    Next

    End Sub

    I've checked all style names are correct so not sure why it doesn't change the style applied to the hyperlink and then remove the hyperlink from the text? The main script I need is the one you have already got working for me - so the 'deleting hyperlinks based on style' would be a 'nice to have'.

    Thanks again. HJ

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2012-02-20T02:30:28+00:00

    It works over here, but skips some hyperlinks and modifies the format of others.

    The reason is, you cannot change the members of a collection while accessing the collection. Word gets confused: as soon as you delete one hyperlink, the collection has one less member but the internal links are not updated.

    This the original document I started with:

    Running your macro resulted in this:

    Be must run through the collection by counting its members, and start from the end to that it won't matter if we've deleted a member, as we've already processed it:

    For i = ActiveDocument.Hyperlinks.Count To 1 Step -1

    If ActiveDocument.Hyperlinks(i).Range.Style = "H4 Small Blue Hyperlink WT" Then

    ActiveDocument.Hyperlinks(i).Range.Style = "H4 Smll WT"

    ActiveDocument.Hyperlinks(i).Delete

    End If

    Next

    This is that I get with the above code:

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2012-02-20T01:47:39+00:00

    Actually it would be good to also have a script that deletes all hyperlinks (regardless of styles) UNLESS the text that is Hyperlinked begins with "<www.".

    So I'd need a script something like:

    Sub DelHyperlink()

    Dim hl As Hyperlink

    For Each hl In ActiveDocument.Hyperlinks

    If  text that is hyperlinked DOES NOT start with <www. then

    ActiveDocument.Hyperlinks(1).Delete

    End If

    Next

    End Sub

    Thanks again. HJ

    Was this answer helpful?

    0 comments No comments