Consulting

Results 1 to 4 of 4

Thread: Exact Match

  1. #1

    Exact Match

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Thanks, really appreciate your help once again (so logical!). Whatever you do for a day job i hope they pay you well.

  4. #4
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •