希望你一切都好。我需要 ms-word 中的一个宏来查找括号之间的所有内容,并将其转换为超链接,其中字符串为“[Pmcid:”the thing”]”,地址是一个末尾带有“the thing”的网址.
到目前为止,我发现通过使用在搜索中使用通配符
([\(])(?@)([\)])
在查找部分,您可以找到括号之间的所有内容,但我不知道如何在宏中使用它并创建超链接。如果您能帮我解决这个问题,我将不胜感激。
提前致谢。
Sub AddHyperlinksToParentheses()
Dim doc As Document
Dim rng As Range
Dim match As Range
Dim searchText As String
Dim linkText As String
Dim linkAddress As String
Const BASE_URL = "https://www.w3schools.com/EXCEL/"
' Set the document
Set doc = ActiveDocument
Set rng = doc.Content
' Use Find to search for text in parentheses
With rng.Find
.ClearFormatting
.Text = "\(*\)"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
Do While .Execute
Set match = doc.Range(rng.Start + 1, rng.End - 1)
' match.Select
searchText = match.Text
' Create the display text and URL
linkText = "[Pmcid:""" & searchText & """]"
linkAddress = BASE_URL & searchText
' Add the hyperlink
doc.Hyperlinks.Add _
Anchor:=match, _
Address:=linkAddress, _
TextToDisplay:=linkText
rng.Collapse Word.wdCollapseEnd
Loop
End With
End Sub