0

从一份新工作开始,我必须阅读我的前任留下的大量文件。它们是包含数百项专利信息的 MS Word 文件。我不想在在线表格中复制/粘贴每个专利号,而是想用可点击的超链接替换所有专利号。我想这应该用 vbscript 来完成(我不习惯使用 MS Office)。

我到目前为止:

<obsolete>

这对我不起作用: 1. 我(可能)需要添加一些东西来循环 ActiveDocument 2. 替换函数可能需要一个字符串而不是参数的对象 - vbscript 中是否有 __toString() ?

谢谢!

更新:我有这个部分工作(正则表达式和查找匹配项) - 现在只要我能正确获得 hyperlink.add-method 的锚点......

Sub HyperlinkPatentNumbers()
'
' HyperlinkPatentNumbers Macro
'

Dim objRegExp, Matches, match, myRange

Set myRange = ActiveDocument.Content

Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
    .Global = True
    .IgnoreCase = False
    .Pattern = "(WO|EP|US)([0-9]*)(A1|A2|B1|B2)"
End With

Set Matches = objRegExp.Execute(myRange)

If Matches.Count >= 1 Then
    For Each match In Matches
        ActiveDocument.Hyperlinks.Add Anchor:=objRegExp.match, Address:="http://worldwide.espacenet.com/publicationDetails/biblio?DB=EPODOC&adjacent=true&locale=en_EP&CC=$1&NR=$2&KC=$3"
    Next
End If

Set Matches = Nothing
Set objRegExp = Nothing

End Sub
4

2 回答 2

0

这是 VBA 还是 VBScript?在 VBScript 中,您不能声明类似 的类型Dim newText As hyperLink,但每个变量都是一个变体,所以:仅此Dim newText而已。

objRegEx.Replace返回带有替换的字符串,并且需要传递两个参数:原始字符串和要替换模式的文本:

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = False
objRegEx.Pattern = "^(WO|EP|US)([0-9]*)(A1|A2|B1|B2)$"

' assuming plainText contains the text you want to create the hyperlink for
strName = objRegEx.Replace(plainText, "$1$2$3")
strAddress = objRegex.Replace(plainText, "http://worldwide.espacenet.com/publicationDetails/biblio?DB=EPODOC&adjacent=true&locale=en_EP&CC=$1&NR=$2&KC=$3"

现在您可以使用strNamestrAddress创建超链接。
专业提示:您可以使用objRegEx.Test(plainText)来查看正则表达式是否匹配任何内容,以便及早处理错误。

于 2013-02-15T14:47:33.437 回答
0

问题解决了:

Sub addHyperlinkToNumbers()

Dim objRegExp As Object
Dim matchRange As Range
Dim Matches
Dim match

Set objRegExp = CreateObject("VBScript.RegExp")

With objRegExp
    .Global = True
    .IgnoreCase = False
    .Pattern = "(WO|EP|US|FR|DE|GB|NL)([0-9]+)(A1|A2|A3|A4|B1|B2|B3|B4)"
End With

Set Matches = objRegExp.Execute(ActiveDocument.Content)

For Each match In Matches
    'This doesn't work, because of the WYSIWYG-model of MS Word:
    'Set matchRange = ActiveDocument.Range(match.FirstIndex, match.FirstIndex + Len(match.Value))

    Set matchRange = ActiveDocument.Content
    With matchRange.Find
        .Text = match.Value
        .MatchWholeWord = True
        .MatchCase = True
        .Wrap = wdFindStop
        .Execute
    End With

    ActiveDocument.Hyperlinks.Add Anchor:=matchRange, _
        Address:="http://worldwide.espacenet.com/publicationDetails/biblio?DB=EPODOC&adjacent=true&locale=en_EP&CC=" _
        & match.Submatches(0) & "&NR=" & match.Submatches(1) & "&KC=" & match.Submatches(2)

Next

MsgBox "Hyperlink added to " & Matches.Count & " patent numbers"

Set objRegExp = Nothing
Set matchRange = Nothing
Set Matches = Nothing
Set match = Nothing

End Sub
于 2013-02-18T12:38:59.307 回答