Consulting

Results 1 to 10 of 10

Thread: "Search and Replace Regular Expressions"

  1. #1

    "Search and Replace Regular Expressions"

    I am an Excel guy and Word really trips me up. I am trying to reorder references in a technical document. When it is created, they are just placed randomly (as in CURRENT below). But I need to reorder them such that the "R" value (always a capitol R followed by a number) appear in order (FINAL DESIRED below). When reordered, they need to be linked up properly within the document. I think what needs to happen is that should go through an an intermediate step (INTERMEDIATE below) to avoid conflicts and then do a find/replace to final desired order. I think the process would go something like this:

    - Find the first "R"+number (if less than 1000). For example, it could be R5.
    - Rename R5 to R1001 and replace all other instances of R5 with R1001
    - Start at beginning of document and find next "R"+number (if less than 1000) and change it to R1002. For the example, R8 would go to R1002.
    - Go through this process until there are no "R"+number less than R1000
    - Finally, rename R1001 to R1, R1002 to R2, etc. (I will manually add these).

    I can donate $40 to the forum for this help.

    -------------------------------------------------

    CURRENT:
    example text [R5, R8] more test text [R33,R2] and still some more [R1] but not done yet [R8, R2, R33] and [R2, R5] plus [R9]

    ---------------

    FINAL DESIRED:
    example text [R1, R2] more test text [R3,R4] and still some more [R5] but not done yet [R2, R4, R3] and [R4, R1] plus [R6]

    ------------

    INTERMEDIATE:
    example text [R1001, R1002] more test text [R1003,R1004] and still some more [R1005] but not done yet [R1002, R1004, R1003] and [R1004, R1001] plus [R1006]
    Last edited by ronjon65; 10-17-2015 at 03:07 PM.

  2. #2
    Your request is somewhat confusing. You claim that you want to get the Final text from the Current text but that doesn't explain how the solitary [R1] in the middle of the first line has become [R5] in the second list. Surely the first R number is R1 followed by R2, R5, R8, R9 and R33?

    CURRENT:
    example text [R5, R8] more test text [R33,R2] and still some more [R1] but not done yet [R8, R2, R33] and [R2, R5] plus [R9]

    ---------------

    FINAL DESIRED:
    example text [R1, R2] more test text [R3,R4] and still some more [R5] but not done yet [R2, R4, R3] and [R4, R1] plus [R6]

    As these are references will they still match the references when renumbered? I would have thought that sorting the numbers in their groups might have been more appropriate? i.e,

    example text [R5, R8] more test text [R2, R33] and still some more [R1] but not done yet [R2, R8, R33] and [R2, R5] plus [R9]
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    What happens in the body is that you want to have the references in order of appearance in the body text. When I write the document, they end up becoming random after enough modification. So for this case, the what would happen is the following. I *think* this would happen if you went through the mentioned steps, but not 100% sure.

    [R5] --> [R1]
    [R8] --> [R2]
    [R33] --> [R3]
    [R2] --> [R4]
    [R1] --> [R5]

    Then at the end of the document, the reference list becomes unsorted and will appear randomly. For example, it might be as follows. But that is no big deal and can be manually sorted.

    [R3]
    [R2]
    [R5]
    [R1]
    [R4]

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I can't help with "Find.. Find Next" in Word, but a Dictionary will suffice


    Possible Algorithm: (R_References is a Dictionary Object)
    Do
    On Error Resume next
    R_References.AddItem = Find("R*")
    Loop Until "R*" Not Found

    For i = 1 to R_references.Count
    Do
    Find and Replace R_references(i) With "R" & i
    Loop Until R_Refernces.Item Not Found
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Running Word 2003 an get these errors:

    R_references.AddItem = Find("R*") -----> Sub or Function not Defined for "Find"
    Find and Replace R_references(i) With "R" & i -----> Shows up Red

    ----

    I think another thing that might be a bit tricky is just identifying the correct number following the R. For example one needs to distinguish between "R1" and "R11".

    I can bump this to a $70 donation or paid directly to someone.

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    An Algorithm is not code. It is merely design aid.

    I think another thing that might be a bit tricky is just identifying the correct number following the R. For example one needs to distinguish between "R1" and "R11".
    I think I will change the Thread Title to "Search and Replace Regular Expressions"

    As to pay, please see my signature block below.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    The following will do what you ask - at least with the example text. Donations may be made to the link on my web site http://www.gmayor.com

    Option Explicit
    
    Sub Macro1()
    Dim oRng As Range, oFind As Range
    Dim iCount As Long
    Dim sText As String, strExt As String
    Dim bFound As Boolean
        iCount = 0
        Do
            bFound = False
            Set oRng = ActiveDocument.Range
            With oRng.Find
                Do While .Execute(FindText:="(R[0-9]{1,})([\],])", MatchWildcards:=True) = True
                    If Val(ExtractDigits(oRng.Text)) <= 1000 Then
                        bFound = True
                        Exit Do
                    End If
                Loop
            End With
            If Not bFound Then GoTo lbl_Restore
    
            Set oRng = ActiveDocument.Range
            With oRng.Find
                Do While .Execute(FindText:="(R[0-9]{1,})([\],])", MatchWildcards:=True) = True
                    If Val(ExtractDigits(oRng.Text)) <= 1000 Then
                        iCount = iCount + 1
                        sText = oRng.Text
                        sText = Trim(Replace(sText, ",", ""))
                        sText = Trim(Replace(sText, "]", ""))
                        Set oFind = ActiveDocument.Range
                        With oFind.Find
                            .Text = sText & "([!0-9])"
                            .Replacement.Text = "R" & iCount + 1000 & "\1"
                            Do While .Execute(MatchWildcards:=True, Replace:=wdReplaceAll)
                            Loop
                        End With
                    End If
                Loop
            End With
        Loop
    lbl_Restore:
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Text = "(R[0-9]{1,})([\],])"
            .Replacement.Text = "\1"
            Do While .Execute(FindText:="(R[0-9]{1,})([\],])", MatchWildcards:=True) = True
                strExt = Right(oRng.Text, 1)
                If Val(ExtractDigits(oRng.Text)) > 1000 Then
                    oRng.Text = "R" & CStr(Val(ExtractDigits(oRng.Text)) - 1000) & strExt
                End If
                oRng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    
    Private Function ExtractDigits(strText As String) As String
    'Graham Mayor
    Dim i As Integer
        ExtractDigits = ""
        For i = 1 To Len(strText)
            If Mid(strText, i, 1) >= "0" And _
               Mid(strText, i, 1) <= "9" Then
                ExtractDigits = ExtractDigits + Mid(strText, i, 1)
            End If
        Next
    lbl_Exit:
        Exit Function
    End Function

  8. #8
    This works great! Amazing job A request though:

    *Can you in a case where something like [R11-] is properly found, meaning that it detects a "-" (like it currently detects a "," and a "]"). This goes along with the next request.
    *Can you also have it ensure that the R is capitalized (hard for me to tell) and the the R is not italicized? The reason is that there are some R values followed by a number, but those are variables (not references), but they are also italicized in the body.

    -----------------
    Separate new request, new subroutine ($30 donation)

    Can you "expand" a scenario where there is a dash, like this [R5-R9] to go to [R5, R6, R7, R8, R9]. This will be helpful to work with the previous subroutine. Note that in this case, there is a space between the R5, and R6. This makes is easier to read. I tried without the space (for convenience) but it is rather difficult to see with the font I am using.

    -----------------
    Separate new request, new subroutine ($40 donation)

    Can you "contract" a scenario where something like this [R5, R6, R7, R8, R9] would to go this [R5-R9]. The contraction would only be for a scenario with 3 or more references though. For example [R5, R6] would not be contracted, but [R5, R6, R7] would contact to [R5-R7]. Also, it would need to check that numbers were increasing order so that something like [R5,R7,R8] would NOT be contracted. I think this will be more difficult that the previous request though.

    NOTE: For this last request, make it a low priority since I may not use it anyway. Originally I wanted to contract the references, but I think I prefer having the references (when grouped like this) in the order of importance as opposed to order of appearance. Therefore, this contraction (if it is done) will probably be done manually instead of automatically. However, the expansion would still be useful if changes are made.
    Last edited by ronjon65; 10-19-2015 at 01:16 PM.

  9. #9
    Just want to say that I was able to use program to organize several hundred references. It worked perfect. It would have taken a long time to do this manually (and significant chance of error). Thanks very much. This code is tremendously useful to me!

  10. #10
    Contact me via my web site. I need to see a sample before investigating your other requests - use the address supportATgmayor.com and put your username in the subject line.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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