A family of Microsoft word processing software products for creating web, email, and print documents.
There are several major changes from the previous macro to the one below.
- The wildcard expressions from your post are listed in the string variable strPatterns, each separated from the next by a "pipe" character "|". (The ampersand and underscore at the ends of the first four lines of that statement are the string concatenation character and the line continuation character. The result is one long string. If copying and pasting the code into your macro editor inserts blank lines between the code lines in this group, you must remove them to prevent a syntax error.)
- The strPatterns string is then loaded into the arrPatterns array by the Split function, which breaks the string at each pipe character.
- Since you mentioned that the last two expressions need a different URL, these are put into the variables strLinkAddr1 and strLinkAddr2. You'll have to insert the proper URL for strLinkAddr2.
- A For...Next loop runs through all the entries in arrPatterns. (The array indexes start at 0, not at 1. The UBound function returns the index of the last entry.) The If...Else...End If structure chooses the correct URL from the current value of idx.
- The MatchWildcards member of the Find object must be set to True so the search expressions will work.
- As indicated in the "don't add a hyperlink" comment, the name of the style applied to the found range is checked, and the hyperlink is applied only if the style is not already "Hyperlink".
- The Replace function removes any space characters that exist in the hyperlink's address.
The following code has been tested against the simple example given in your post. You will need to test it on a live document (or a copy of one).
Option Explicit
Sub MakeHyperlinks()
Dim rg As Range
Dim strLinkAddr
Dim strLinkAddr1 As String
Dim strLinkAddr2 As String
Dim strPatterns As String
Dim arrPatterns() As String
Dim idx As Integer
'list of all wildcard expressions
' NOTE: for the first two pairs of patterns, the one with the "Add" or "Resumption"
' must come before the one that does not have that part
strPatterns = "[AS]/[0-9]{1,}/[0-9]{1,}/Add.[0-9]{1,}|[AS]/[0-9]{1,}/[0-9]{1,}|" & _
"S/PV.[0-9]{1,} \(Resumption [0-9]{1,}\)|S/PV.[0-9]{1,}|" & _
"S/INF/[0-9]{1,}/[0-9]{1,}|S/PRST/[0-9]{1,}/[0-9]{1,}|" & _
"ST/PSCA/[0-9]{1,}/Add.[0-9]{1,}|[0-9]{1,} \([0-9]{4}\)|" & _
"[0-9]{1,} \([IVX]{1,}\)"
'load the expressions into an array
arrPatterns = Split(strPatterns, "|")
strLinkAddr1 = "https://dtf.custhelp.com/AgentWeb/api/contextmanager/v.32201260002/workspaceContext/workspace/processedPrint/Answer/"
strLinkAddr2 = "//someotherplace.com" '*** insert correct URL here
'loop through the patterns in the array
For idx = 0 To UBound(arrPatterns)
'select second URL for the last two patterns
If idx < UBound(arrPatterns) - 1 Then
strLinkAddr = strLinkAddr1
Else
strLinkAddr = strLinkAddr2
End If
Set rg = ActiveDocument.Range
With rg.Find
.MatchWildcards = True
.Text = arrPatterns(idx) ' choose the pattern for this pass
.Wrap = wdFindStop
While .Execute
'don't add a hyperlink to a range that is already a hyperlink
'for example, A/1234/1234/Add.1 would already have been linked
'when A/1234/1234 is found inside it, but it would have Hyperlink style
If Not rg.Style = "Hyperlink" Then
rg.Hyperlinks.Add Anchor:=rg, Address:=strLinkAddr & Right(rg.Text, 5)
'remove any spaces
rg.Hyperlinks(1).Address = Replace(rg.Hyperlinks(1).Address, " ", "")
rg.Collapse wdCollapseEnd
End If
DoEvents
Wend
End With
Next idx
End Sub