PDA

View Full Version : Word Range issue



edyau
11-28-2007, 11:58 AM
Hi kind people,

Please can you help?!
I'm trying to write a subroutine to pick up web addresses and email addresses and hyperlink them.

I pass in a regex like \b[a-zA-Z\.-]*\@[a-zA-Z-]*.com\b", find each match, then hyperlink it.

I'm have 1 issue in particular:
1) The regex will do a match, but when I try and set the range to match what has been found, it sometimes seems to be out-of-sync. It's just a few characters ahead of where it should be, but that's enough to mess things up.
I've been trying to debug this by looking at the various values for the range start and end, but the numbers don't really make much sense.

Maybe it's my misunderstanding of how the range object works......I'm hoping someone out there can point me in the right direction.

The subroutine that is causing the issue is below, but I have attached the word doc containing all my code in a zip so that you can try it out for yourself. To test, run test().


Sub addHyperlinks(regexStr As String, totalMatches As Integer)
Dim wordcount As Integer
Dim regex As RegExp
Dim rngToSrch, nxtRngToSrch, rngResult As Range
Dim Matches As MatchCollection

Set regex = New RegExp ' Create a regular expression.
regex.Pattern = regexStr
regex.IgnoreCase = False ' Set case insensitivity.
regex.Global = False ' Set global applicability.

Set rngToSrch = ActiveDocument.Range
Set nxtRngToSrch = rngToSrch.Duplicate
Set rngResult = rngToSrch.Duplicate

Dim matched As Integer
matched = 1
searchStart = 0

Do
Set Matches = regex.Execute(nxtRngToSrch.Text) ' Execute search.

matched = Matches.Count 'check if there are any matches
If matched = 0 Then Exit Do
'selects a range from the index of the character to the index of the character plus the length of the word

With Matches.Item(0)
'set the result to what has been found
nxtRngToSrch.MoveStart Unit:=wdCharacter, Count:=.FirstIndex
nxtRngToSrch.End = nxtRngToSrch.Start + Len(.Value)
Set rngResult = nxtRngToSrch.Duplicate

wordcount = countVBWords(rngResult) 'count the words we need to move the range

rngResult.Select

If rngResult = "" Then
Exit Do
Else
'make text hyperlink
ActiveDocument.Hyperlinks.Add Anchor:=rngResult, Address:= _
rngResult.Text, SubAddress:=rngResult.Text, ScreenTip:=rngResult.Text, TextToDisplay:= _
rngResult.Text

totalMatches = totalMatches + 1
End If

On Error GoTo ErrorHandler

End With

'prepare for next search
nxtRngToSrch.MoveStart Unit:=wdWord, Count:=wordcount
nxtRngToSrch.End = rngToSrch.End
Loop

ErrorHandler:

End Sub

TonyJollans
11-28-2007, 01:27 PM
* Select Format > AutoFormat... from the Menu
* Click on Options and UNcheck everything EXCEPT "Internet and network paths with hyperlinks"
* Click OK (to save the changes to the options)
* Click OK (to do the Autoformat)

* Select Tools > Autocorrect Options... > Autoformat tab from the Menu
* Reset your Autoformat options as they were (or as you want them)
* Click OK again

No need for code, but you could record it.

edyau
11-29-2007, 05:11 AM
Hi, Thanks!

It works perfectly....spend days getting my head round this, till it almost works, then find there's a simple as pie solution that would have taken 30 mins....funny funny!

TonyJollans
11-29-2007, 06:10 AM
Even with what I know, I still find that from time to time. Look on the bright side - next time you want to use regex, you'll have a flying start :)