Consulting

Results 1 to 3 of 3

Thread: Word Series Find and Replace

  1. #1

    Word Series Find and Replace

    This is tricky.

    Situation: I have a code that has repeating elements that I need to clean using VBA.

    Example:

    In the Parse tree below, the elements of parse tree (like S, DT, NN)and each subsequent occurrence needs to be incremented by one, automatically.


    Input:
    (S
    (NP (DT ) (NN ))
    (VP (VBZ )
    (NN (DT )))
    (. .)))

    Output expected

    (S
    (NP (DT ) (NN ))
    (VP (VBZ )
    (NN1 (DT1 )))
    (. .)))

    If another DT or NN occurs it is identified as DT2 or NN2.

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim arrFind() As String
    Dim oRng As Range
    Dim lngIndex As Long, lngCounter As Long
      arrFind = Split("(NN ,(DT ", ",")
      For lngIndex = 0 To UBound(arrFind)
        Set oRng = ActiveDocument.Range
        lngCounter = 0
        With oRng.Find
          .Text = arrFind(lngIndex)
          While .Execute
            If lngCounter > 0 Then
              oRng.End = oRng.End - 1
              oRng.Text = oRng.Text & lngCounter
            End If
            lngCounter = lngCounter + 1
            oRng.Collapse wdCollapseEnd
          Wend
        End With
      Next
    lbl_Exit:
      Exit Sub
      
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Quote Originally Posted by gmaxey View Post
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim arrFind() As String
    Dim oRng As Range
    Dim lngIndex As Long, lngCounter As Long
      arrFind = Split("(NN ,(DT ", ",")
      For lngIndex = 0 To UBound(arrFind)
        Set oRng = ActiveDocument.Range
        lngCounter = 0
        With oRng.Find
          .Text = arrFind(lngIndex)
          While .Execute
            If lngCounter > 0 Then
              oRng.End = oRng.End - 1
              oRng.Text = oRng.Text & lngCounter
            End If
            lngCounter = lngCounter + 1
            oRng.Collapse wdCollapseEnd
          Wend
        End With
      Next
    lbl_Exit:
      Exit Sub
      
    End Sub
    Thanks. Fantastic code.

Tags for this Thread

Posting Permissions

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