A family of Microsoft word processing software products for creating web, email, and print documents.
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:
- For all hyperlinks, select each one
- 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).
- 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).
- 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:
- Select once again the hyperlink
- Apply the proper character style (here, called "Red")
- 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