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

时间:2015-04-20 11:39:48

标签: ms-word word-vba

如何查找超链接文本并替换为以下示例的嵌入文本?

例如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>

E.g。 2:

www.google.com
www.stackoverflow.com

E.g。 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>

E.g。 2应转换为:

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

E.g。 2应转换为:

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

1 个答案:

答案 0 :(得分:0)

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

    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