2013-02-15 117 views
0

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

我到目前为止有:

<obsolete> 

这不是为我工作: 1.我(可能)需要通过向的ActiveDocument添加的东西循环 2.更换功能可能需要一个串而不是一个参数的对象 - 是否有一个VBScript中的__toString()?

THX!

更新: 我有这部分工作(正则表达式,并找到比赛) - 现在只要我能得到的hyperlink.add法右锚...

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 

回答

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 
+0

事实上,我不得不在正则表达式被选为范围的部分中找出一个小错误。我会更新我的答案,使其成为完整的工作代码。 – zenlord 2013-02-18 15:21:39

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)来查看正则表达式是否匹配任何错误的早期处理。

+0

THX - 它没有工作开箱即用,但我搜索了一下进一步,我已经更新了我的第一篇文章。你可以看看吗? – zenlord 2013-02-15 17:08:39

+0

VBScript无法处理像'Anchor:= foo'这样的命名参数。尝试使用参数位于固定位置的本地版本:'ActiveDocument.Hyperlinks.Add锚点,地址,子地址,屏幕提示,文本显示'。 – AutomatedChaos 2013-02-15 18:56:55

+0

仅供参考:'如果Matches.Count> = 1那么'不需要,因为如果没有匹配,执行将通过'For Each Match In Matches'而不处理内部语句。 – AutomatedChaos 2013-02-15 18:58:24