最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - How to create a macro in MS Word that will add angle brackets around each hyperlink? - Stack Overflow

programmeradmin1浏览0评论

This is my first question, so please let me know if the format is wrong in any way.

I am working for a publishing house that, as part of its style guide, requires all URLs to appear within angle brackets, that is, <www.LINK> instead of www.LINK.

Since doing this manually is tiresome, I tried to use the Record Macro feature to create a macro that would do this.

Unfortunately, all I've been able to do so far is find every instance of a hyperlink (by searching for "^d" in the Find window). This macro also has the downside of highlighting the entire hyperlink rather than the start and end separately.

Can anyone help me on how to create a macro that would automatically 1) find the start of a hyperlink, 2) add < before it, 3) find the end of the hyperlink, and 3) add > after it?

This is my first question, so please let me know if the format is wrong in any way.

I am working for a publishing house that, as part of its style guide, requires all URLs to appear within angle brackets, that is, <www.LINK> instead of www.LINK.

Since doing this manually is tiresome, I tried to use the Record Macro feature to create a macro that would do this.

Unfortunately, all I've been able to do so far is find every instance of a hyperlink (by searching for "^d" in the Find window). This macro also has the downside of highlighting the entire hyperlink rather than the start and end separately.

Can anyone help me on how to create a macro that would automatically 1) find the start of a hyperlink, 2) add < before it, 3) find the end of the hyperlink, and 3) add > after it?

Share Improve this question asked Mar 3 at 13:31 peterdhirpa13peterdhirpa13 11 bronze badge
Add a comment  | 

3 Answers 3

Reset to default 2

Just loop over the Hyperlink-collection of a document and modify the TextToDisplay-property.

Sub decorateHyperlinks(doc As Document)
    If doc Is Nothing Then Exit Sub

    Dim link As Hyperlink
    For Each link In doc.Hyperlinks
        If Left(link.TextToDisplay, 1) <> "<" Then
            link.TextToDisplay = "<" & link.TextToDisplay
        End If
        If Right(link.TextToDisplay, 1) <> ">" Then
            link.TextToDisplay = link.TextToDisplay & ">"
        End If
    Next link
End Sub

If you put this code into Normal.Dotm, add a parameterless routine that you can call from the Macro-menu:

Sub decorateHyperlinksOfActiveWorkbook()
    decorateHyperlinks
End Sub

You can loop and replace the content of all hyperlinks in the document, you can find more information about it here : Microsoft VBA Hyperlinks

and you can achieve the output you want a bit like

Sub HypVBA()
Dim lien As Hyperlink

For Each lien In ActiveDocument.Hyperlinks
    lien.Range.InsertBefore "<"
    lien.Range.InsertAfter ">"
Next lien

End Sub

An alternative to adding brackets without using a loop.

Sub HyperLinkReplacement()
    With ActiveDocument.Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Style = ActiveDocument.Styles("Hyperlink")
        .Replacement.Text = "<^&>" ' add <>
        .Execute Replace:=wdReplaceAll
    End With
End Sub

发布评论

评论列表(0)

  1. 暂无评论