PDA

View Full Version : Exact Match



Bullracer2
06-02-2013, 08:42 PM
Hi I have a macro which runs through a documents and sets hyperlinks on the text it finds. I'm finding in a number of situations that the wrong hyperlink is being set. This is because it if first finding a subset of a longer string. I.e in the example below ABC 101 is being set with the hyperlink for ABC 1.

FndArray = Array("<ABC 1.[0-9]{1,}>", "ABC 1",<ABC 101.[0-9]{1,}>", "ABC 101 ",

I am using wildcards as in some instances ABC 1 may be ABC 1.10, etc. I found putting a space after the "ABC 1 " eliminated the issuse but caused another where there was another character after it. I.e. a . or ).

Rest of the code is as follows:

With wdDoc.Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = FndArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
.Execute
End With
Do While .Find.Found = True
.Duplicate.Hyperlinks.Add Anchor:=.Duplicate, Address:=RepArray(i), _
SubAddress:="", ScreenTip:="", TextToDisplay:=""
.Start = .Duplicate.Hyperlinks(1).Range.End
.Find.Execute
Loop
End With

Hoping someone is able to assist me.

Thanks.

macropod
06-02-2013, 11:39 PM
Ideally, you'd start with the longest strings. That way, only subsequent passes you can check whether the found string is already part of a hyperlink.

Bullracer2
06-10-2013, 04:45 PM
Thanks, really appreciate your help once again (so logical!). Whatever you do for a day job i hope they pay you well.

Bullracer2
06-16-2013, 11:46 PM
Been playing around this for a while without much success so thought i'd reach out for help again.

I changed the order in which the array searched for text, which in terms of highlighting the entire text worked.
However they hyperlink was being changed on the subsequent pass of the identified text.

So to explain a little better. I have ABC 101, ABC 10, AND ABC 1. The macro identifies the text and applies the hyperlink. So where ABC 101 is identified the hyperlink is applied, but when the macro continues the loop, the hyperlink to ABC 10 and then subsequently ABC 1 is applied to the original ABC 101 text with hyperlink.

I think i need to have a test in there to determine if a hyperlink already exists? was looking at trying to identify the style but really not sure the best aproach.

With wdDoc.Content
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = FndArray(i)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
.Execute
End With
Do While .Find.Found = True
.Duplicate.Hyperlinks.Add Anchor:=.Duplicate, Address:=RepArray(i), _
SubAddress:="", ScreenTip:="", TextToDisplay:=""
.Start = .Duplicate.Hyperlinks(1).Range.End
.Find.Execute
Loop
End With