Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find and Append with nested increments

    Hi would like to do following.....I have document with

    REQ0012
    REQ0013
    REQ0013.1
    REQ0013.2
    REQ0013.2.1
    REQ0013.2.2.1
    REQ0014
    REQ0015

    I would like to make this document as below.

    [CCC.0012](PAX) REQ0012
    [CCC.0013](PAX) REQ0013
    [CCC.0013.1](PAX) REQ0013.1
    [CCC.0013.2](PAX) REQ0013.2
    [CCC.0013.2.1](PAX) REQ0013.2.1
    [CCC.0013.2.2.1](PAX) REQ0013.2.2.1
    [CCC.0014](PAX) REQ0014
    [CCC.0015](PAX) REQ0014


    Following is the code I have so far. I am not sure how to detect the "." and create the nesting....

    Appreciate the kind help.

    Code:
    Sub Test()
    
        Dim iCount As Long, strSearch As String, strReplace As String, strPacket As String
    
        strSearch = UCase(InputBox("Search", "SS", "REQ"))
    
        strReplace = UCase(InputBox("Req #", "Req", "CCC.0000"))
    
        strPacket = UCase(InputBox("Pax #", "PAX", "PAX"))
        iCount = 0
        With ActiveDocument.Content
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = strSearch & "[0-9]{4}"
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchWildcards = True
                .Execute
            End With
      
            While .Find.Found = True
                iCount = iCount + 1
                .Text = "[" & strReplace & "." & Format(iCount, "000") & "](" & strPacket & ") " & .Text
                .Collapse wdCollapseEnd
                .Find.Execute
            Wend
        End With
    
    End Sub

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You don't need a macro for this - you can do the lot with a single wildcard Find/Replace, where:
    Find = REQ([0-9.]{1,})
    Replace = [CCC.\1](PAX) ^&
    You could, of course, turn the above into a macro (e.g. via the macro recorder).
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,

    Thank you for your kindness. This helps. But I have made a minor error when asking the question... I want the result to be as follows...

    [CCC.0001](PAX) REQ0012
    [CCC.0002](PAX) REQ0013
    [CCC.0002.1](PAX) REQ0013.1
    [CCC.0002.2](PAX) REQ0013.2
    [CCC.0002.2.1](PAX) REQ0013.2.1
    [CCC.0002.2.2.1](PAX) REQ0013.2.2.1
    [CCC.0003](PAX) REQ0014
    [CCC.0004](PAX) REQ0014

    Also please note that this is just an example and I was trying to do a macro as I have these in nearly 200 of documents. Following is the structure and "CCC" and "PAX" could change but the nesting of the numbers would be the same. appreciate your kind help

    Quote Originally Posted by macropod View Post
    You don't need a macro for this - you can do the lot with a single wildcard Find/Replace, where:
    Find = REQ([0-9.]{1,})
    Replace = [CCC.\1](PAX) ^&
    You could, of course, turn the above into a macro (e.g. via the macro recorder).
    Last edited by woow14610; 2014-04-12 at 08:52.

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub UpdateTags()
    Application.ScreenUpdating = False
    Dim i As Long, j As Long, StrRep As String, StrTmp As String
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "REQ[0-9.]{1,}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        StrTmp = Split(.Text, ".")(0)
        If CLng(Right(StrTmp, 4)) > i Then
          i = CLng(Right(StrTmp, 4))
          j = j + 1
        End If
        StrRep = "[CCC." & Format(j, "0000")
        If UBound(Split(.Text, ".")) > 0 Then
          StrRep = StrRep & Replace(.Text, StrTmp, "")
        End If
        .InsertBefore StrRep & "](PAX) "
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again....Result was not quit as expected....

    [CCC.0001](PAX) REQ0012
    [CCC.0001](PAX) REQ0013
    [CCC.0001.1](PAX) REQ0013.1
    [CCC.0001.2](PAX) REQ0013.2
    [CCC.0001.2.1](PAX) REQ0013.2.1
    [CCC.0001.2.2.1](PAX) REQ0013.2.2.1
    [CCC.0001](PAX) REQ0014
    [CCC.0001](PAX) REQ0015


    If you see the top number stays at CCC.0001
    Last edited by macropod; 2014-04-13 at 00:54. Reason: Deleted unnecessary quote of entire post replied to

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by woow14610 View Post
    Thanks again....Result was not quit as expected....
    ...
    If you see the top number stays at CCC.0001
    Not when I run the macro on the sample you provided! With those data I get:

    [CCC.0001](PAX) REQ0012
    [CCC.0002](PAX) REQ0013
    [CCC.0002.1](PAX) REQ0013.1
    [CCC.0002.2](PAX) REQ0013.2
    [CCC.0002.2.1](PAX) REQ0013.2.1
    [CCC.0002.2.2.1](PAX) REQ0013.2.2.1
    [CCC.0003](PAX) REQ0014
    [CCC.0004](PAX) REQ0015

    PS: Please don't automatically quote entire posts in your replies. If you need to cite something, quote just that part.
    Last edited by macropod; 2014-04-13 at 01:27.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again. yes it is my mistake. It worked.....I did find 2 issues...I appreciate if you could kindly help....

    1 issue

    So these REQ are sometimes does not increment correctly...As an example...

    REQ9363
    REQ9363.1
    REQ9364.
    REQ9364.1
    REQ9365
    REQ9365.1
    REQ9365.2
    REQ9365.3
    REQ9365.4
    REQ9584
    REQ9366
    REQ9366.1
    REQ9366.3
    REQ9384
    REQ9385

    macro result was....


    [CCC.0001](PAX) REQ9363
    [CCC.0001.1](PAX) REQ9363.1
    [CCC.0002.](PAX) REQ9364.
    [CCC.0002.1](PAX) REQ9364.1
    [CCC.0003](PAX) REQ9365
    [CCC.0003.1](PAX) REQ9365.1
    [CCC.0003.2](PAX) REQ9365.2
    [CCC.0003.3](PAX) REQ9365.3
    [CCC.0003.4](PAX) REQ9365.4
    [CCC.0004](PAX) REQ9584
    [CCC.0004](PAX) REQ9366
    [CCC.0004.1](PAX) REQ9366.1
    [CCC.0004.3](PAX) REQ9366.3
    [CCC.0004](PAX) REQ9384
    [CCC.0004](PAX) REQ9385

    As you could see increment breaks here....
    [CCC.0004](PAX) REQ9584
    [CCC.0004](PAX) REQ9366

    2 isssue
    So These REQ are hidden. I was manually unhidding them to append these new values as or else the macro does not work as it can't find. I want end result to be "[CCC.0004](PAX)" to be unhidden and "REQ9584: to remain hidden.

    I appreciate your help and it would be great if you could help me here.

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It would be helpful if you (a) specified the incrementing requirements; (b) specified the requirements vis--vis hidden/visible text ; and (c) supplied representative data, at the outset. This is the third time you've specified something different.

    Try:
    Code:
    Sub UpdateTags()
    Application.ScreenUpdating = False
    Dim i As Long, j As Long, StrRep As String, StrTmp As String, Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "REQ[0-9.]{1,}"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        StrTmp = Split(.Text, ".")(0)
        If CLng(Right(StrTmp, 4)) <> i Then
          i = CLng(Right(StrTmp, 4))
          j = j + 1
        End If
        StrRep = "[CCC." & Format(j, "0000")
        If UBound(Split(.Text, ".")) > 0 Then
          StrRep = StrRep & Replace(.Text, StrTmp, "")
        End If
        Set Rng = .Duplicate
        With Rng
          .Collapse wdCollapseStart
          .Text = StrRep & "](PAX) "
          .Font.Hidden = False
        End With
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,

    Sorry for the trouble. It was not intentional since there 100s of documents I find it during the process too. Again sorry. So this did work but not the hidden part. I have to manually unhidde the "REQ" section and also after the replacement also all the items remain unhidden as oppose to "[44.001.001](FT-14212)" being unhidden and "REQ" section being hidden.

    Again thanks so much for all the help

  10. #10
    New Lounger
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Paul,

    I got it working. 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
  •