title: vbscript: replace text in activedocument with hyperlink
body: <p>Starting out at a new job and I have to go through a whole lot of documents that my predecessor left. They are MS Word-files that contain information on several hundreds of patents. Instead of copy/pasting every single patent-number in an online form, I would like to replace all patent-numbers with a clickable hyperlink. I guess this should be done with vbscript (I'm not used to working with MS Office).</p>
  
  <p>I have so far:</p>
  
  <pre><code>&lt;obsolete&gt;
  </code></pre>
  
  <p>This is not working for me:
  1. I (probably) need to add something to loop through the ActiveDocument
  2. The replace-function probably needs a string and not an object for a parameter - is there a __toString() in vbscript?</p>
  
  <p>THX!</p>
  
  <p>UPDATE:
  I have this partially working (regex and finding matches) - now if only I could get the anchor for the hyperlink.add-method right...</p>
  
  <pre><code>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 &gt;= 1 Then
      For Each match In Matches
          ActiveDocument.Hyperlinks.Add Anchor:=objRegExp.match, Address:="http://worldwide.espacenet.com/publicationDetails/biblio?DB=EPODOC&amp;adjacent=true&amp;locale=en_EP&amp;CC=$1&amp;NR=$2&amp;KC=$3"
      Next
  End If
  
  Set Matches = Nothing
  Set objRegExp = Nothing
  
  End Sub
  </code></pre>