2013-02-15 118 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