简体   繁体   English

VBA MS Word宏:将嵌入式HTML链接转换为纯文本

[英]VBA ms word macro: convert an embedded HTML link into plain text

I want to add a subroutine into my Word 2007 macro that will take the text and address for an embedded link in a document, and convert it to an href tag that uses the text as the display text, and the address for the HTML address. 我想在我的Word 2007宏中添加一个子例程,该子例程将采用文档中嵌入链接的文本和地址,并将其转换为使用文本作为显示文本的href标记,以及将HTML地址作为地址。

So a line that appears as "go here to get to google" with a blue underline, and when clicked goes to www.google.com, gets replaced with: 因此,显示为“转到此处前往Google”的行带有蓝色下划线,当单击该链接转到www.google.com时,将替换为:

<a target="_blank" href="http://www.google.com">go here to get to google</a>

This routine is to be added to an existing macro that I already have that does some other minor formatting, so no need for headers and whatnot. 该例程将添加到我已经拥有的现有宏中,该宏可以进行其他一些次要的格式化,因此不需要标题和其他内容。

This will print the HTML tags as you specify for all of the links in your document. 这将按照您为文档中所有链接指定的方式打印HTML标记。

Dim hlink As Hyperlink
Dim htmlLink As String

For Each hlink In ThisDocument.Hyperlinks
    With hlink
        htmlLink = "<a target=""_blank"" href=""" & .Address & """>" & _
            .TextToDisplay & "</a>"

        Debug.Print htmlLink
    End With
Next hlink

Of course, you'll want to do something more useful with them than just print them in the Immediate window. 当然,除了将它们打印在“立即”窗口中之外,您还需要对它们进行更多有用的处理。

As an aside, I prefer to use DuckDuckGo in my examples due its much better privacy policy than Google's... 顺便说一句 ,我更喜欢在示例中使用DuckDuckGo ,因为它的隐私权政策比Google的隐私权政策好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM