Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    OR function in Find and Replace?

    Hi guys,

    I've got the following code, which I created with your help a few months ago. It searches a Word document for instances of Doc IDs in a format <[A-Z]{3,}.[0-9]{4}.[0-9]{4}.[0-9]{4}, and then hyperlinks them.

    Code:
    Sub AddHyperlinks()
    Application.ScreenUpdating = False
    Dim wdDoc As Document
      Set wdDoc = ActiveDocument
      'This first loop searches the body of the document 
      With wdDoc.Range
        With .Find
          .ClearFormatting
        'This searches for a reference of XXX+.0000.0000.0000 (where XXX+ is equal to at least 3 letters).  If the references are different to this, change the next line of code.
          .Text = "<[A-Z]{3,}.[0-9]{4}.[0-9]{4}.[0-9]{4}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found
        'Change the web address here to reflect the web address of the current e-file
          .Hyperlinks.Add Anchor:=.Duplicate, Address:="https://[redacted]" & .Text & ".PDF", TextToDisplay:=.Text
          .End = .End + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
        
      'This next loop searches the footnotes, if there are any
      If ActiveDocument.Footnotes.Count >= 1 Then
      With wdDoc.StoryRanges(wdFootnotesStory)
        With .Find
          .ClearFormatting
        'This searches for a reference of XXX+.0000.0000.0000 (where XXX+ is equal to at least 3 letters).  If the references are different to this, change the next line of code.
          .Text = "<[A-Z]{3,}.[0-9]{4}.[0-9]{4}.[0-9]{4}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found
        'Change the web address here to reflect the web address of the current e-file
          .Hyperlinks.Add Anchor:=.Duplicate, Address:="https://[redacted]" & .Text & ".PDF", TextToDisplay:=.Text
          .End = .End + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      End If
        
    Set wdDoc = Nothing
    Application.ScreenUpdating = True
    End Sub

    I've now been told it needs to search for Doc IDs formatted as <[A-Z]{3,}.[0-9]{4}.[0-9]{4}.[0-9]{4} OR <[A-Z]{3,}.[0-9]{4}.[0-9]{4}. I tried simply adding another loop to search for the shorter string first, but that just puts a short string link on the longer strings (ie, if the Doc ID is AAA.0000.0001.0002, it only links as AAA.000.0001), and I then tried doing the shorter string last, but that actually deletes the last part of the Doc ID altogether. Fun fun.

    So, is there a way I can add an OR to the string search? I tried just doing this, but obviously that didn't work!

    Code:
    .Text = "<[A-Z]{3,}.[0-9]{4}.[0-9]{4}.[0-9]{4}" Or "<[A-Z]{3,}.[0-9]{4}.[0-9]{4}"
    What's the simplest way I can do this?

    Thank you!

    C

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,805
    Thanks
    0
    Thanked 166 Times in 153 Posts
    The simplest way is to use:
    <[A-Z]{3,}.[0-9]{4}.[0-9.]{4,9}
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. The Following User Says Thank You to macropod For This Useful Post:

    caracolesa (2014-05-06)

  5. #3
    Lounger
    Join Date
    Dec 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you! Once again, I was trying to get too complicated, and it was something really simple. Thanks!

Posting Permissions

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