如何查找超链接文本并替换为嵌入文本



在以下示例中,我应该如何找到超链接文本并替换为嵌入的文本?

例如 1:

目录

<p><a href="ch1">Introduction</a>. It refers to the corresponding id "ch1"/p>
<p><a href="ch2">Research</a>. It refers to the corresponding id "ch2"</p>
<p><a href="ch3">Results</a>. It refers to the corresponding id "ch3"</p>
<p><a href="ch4">Discussion</a>. It refers to the corresponding id "ch4"</p>
<p><a href="ch5">Conclusion</a>. It refers to the corresponding id "ch5"</p>

例2:

www.google.com
www.stackoverflow.com

例如 3:

引文

*see* <a href="ch1">Chapter. 1</a>. It refers to the id "ch1"
*see* <a href="fig1">Fig. 1</a>. It refers to the id "fig1"
*see* <a href="tab1">Table. 1</a>. It refers to the id "tab1"

上面提到的示例使用 VBA 宏转换为 html <a> 标记。

示例 1 应转换为:

     <a href="ch1">Introduction<a>
     <a href="ch2">Research</a>
     <a href="ch3">Results</a>
     <a href="ch4">Discussion</a>
     <a href="ch5">Conclusion</a>

例如,2 应转换为:

   <a href="http://www.google.com">www.google.com</a>
   <a href="www.stackoverflow.com">www.stackoverflow.com</a>

例如,2 应转换为:

   <a href="http://www.google.com">www.google.com</a>
   <a href="www.stackoverflow.com">www.stackoverflow.com</a>
实际上

,我想从文本中提取超链接。所以我用下面的代码解决了这个问题。

    Sub HlinkChanger()
Dim oRange As Word.Range
Dim oField As Field
Dim link As Variant
With ActiveDocument
.Range.AutoFormat
For Each oRange In .StoryRanges
For Each oFld In oRange.Fields
If oFld.Type = wdFieldHyperlink Then
For Each link In oFld.Result.Hyperlinks
oFld.Select
Selection.InsertBefore "<a href=""" + link.Address + """>"
Selection.InsertAfter "</a>"
Next link
End If
Next oFld
Set oRange = oRange.NextStoryRange
Next oRange
End With
End Sub

最新更新