Consulting

Results 1 to 10 of 10

Thread: Find and Replace, sort of

  1. #1

    Find and Replace, sort of

    Morning all,
    I have a document (sample below) which contains specifications representes by M: or I: and sub specs in the body represented by (M:.x) or (I:.x)
    I have a piece of code that will find these but if possible could someone modify it such that after the colon numbers are added in increments of ten and the sub specs also reflect the spec that they refer.
    and example of how I would like to result is shown at the bottom.

    thanks
       With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Wrap = wdFindStop
            .MatchWholeWord = True
            .MatchWildcards = True
            .MatchCase = True
             'Find expressions between matched pairs of double quotes,
             'allowing for the fact that 'smart quotes' may not be in use.
               .Text = "([MID]{1})([:]{1})([^t)]{1,})"
            .Execute
        End With
    SAMPLE
    M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:

    • Discard any outstanding request. (M:.1)
    • Close the socket connection to the remote RCC. (M:.2)
    • Cancel the poll timer for the connection. (M:.3)
    • Cancel the poll received timer for the connection. (M:.4)



    RESULT
    M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:

    • Discard any outstanding request. (M:10.1)
    • Close the socket connection to the remote RCC. (M:10.2)
    • Cancel the poll timer for the connection. (M:10.3)
    • Cancel the poll received timer for the connection. (M:10.4)

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Adding the '10' between the M: and whatever follows it is trivial - you don't even need a macro for that. However, you also refer to a requirement that "after the colon numbers are added in increments of ten". Your sample data give no indication of when such incrementing occurs or how one might differentiate one set of specs from the others.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Very true I thought this was cleaer than it apparently was,
    SAMPLE
    M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


    • Discard any outstanding request. (M:.1)
    • Close the socket connection to the remote RCC. (M:.2)
    • Cancel the poll timer for the connection. (M:.3)
    • Cancel the poll received timer for the connection. (M:.4)

    M: The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.

    • The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:.1)


    SAMPLE
    M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


    • Discard any outstanding request. (M:10.1)
    • Close the socket connection to the remote RCC. (M:10.2)
    • Cancel the poll timer for the connection. (M:10.3)
    • Cancel the poll received timer for the connection. (M:10.4)


    M: 20 The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.

    • The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:20.1)

  4. #4
    Edit:
    Very true I thought this was cleaer than it apparently was,
    SAMPLE
    M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


    • Discard any outstanding request. (M:.1)
    • Close the socket connection to the remote RCC. (M:.2)
    • Cancel the poll timer for the connection. (M:.3)
    • Cancel the poll received timer for the connection. (M:.4)

    M: The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.

    • The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:.1)


    RESULT
    M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


    • Discard any outstanding request. (M:10.1)
    • Close the socket connection to the remote RCC. (M:10.2)
    • Cancel the poll timer for the connection. (M:10.3)
    • Cancel the poll received timer for the connection. (M:10.4)


    M: 20 The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.

    • The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:20.1)

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It's still not clear where 'I' comes into the scenario. For the data you've provided, you could use:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "M:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .Duplicate.Characters.First.Previous = vbCr Then i = i + 10
        .InsertAfter i
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    MsgBox i / 10 & " specifications updated."
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    the M in the text shown could either be an M or an I in the text, obviously if the if the lead is an I: then all the sub references in the text will also be an I.

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
        Application.ScreenUpdating = False
        Dim i As Long
        With ActiveDocument.Range
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "[MI]:"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchWildcards = True
                .Execute
            End With
            Do While .Find.Found
                If .Duplicate.Characters.First.Previous = vbCr Then i = i + 10
                .InsertAfter i
                .Collapse wdCollapseEnd
                .Find.Execute
            Loop
        End With
        Application.ScreenUpdating = True
        MsgBox i / 10 & " specifications updated."
    End Sub
    Still not clear when the counter increases in a document with both I and M...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    Sub M_snb()
       y = 1
       sn = Split(vbCr & ThisDocument.Content, vbCr & "M:")
    
       For j = 1 To UBound(sn)
          x = UBound(Split(sn(j), vbCr))
          ThisDocument.Range(ThisDocument.Paragraphs(y).Range.Start, ThisDocument.Paragraphs(y + x).Range.End).Find.Execute "M:", , , , , , , , , "M:" & j * 10,2
          y = y + x
       Next
    End Sub

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by snb View Post
    Sub M_snb()
       y = 1
       sn = Split(vbCr & ThisDocument.Content, vbCr & "M:")
    
       For j = 1 To UBound(sn)
          x = UBound(Split(sn(j), vbCr))
          ThisDocument.Range(ThisDocument.Paragraphs(y).Range.Start, ThisDocument.Paragraphs(y + x).Range.End).Find.Execute "M:", , , , , , , , , "M:" & j * 10,2
          y = y + x
       Next
    End Sub
    Once again, snb, ignoring the user's clearly statement requirements. Will you ever learn to address those instead of just showing off???
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    No kidding. snb it is REALLY annoying. Even if it was correct (which in this case it is not) it is super annoying to test because of your - all I can think of is that it is out and out laziness - habit of not declaring variables. Which means the rest of us who DO, have to either comment out Option Explicit or write in our own declarations.

    I do want to test what you post because you clearly do have knowledge of VBA; why are you making it so bloody hard???? I want to see what you are doing but it is getting to the point that I will not because it is so annoying. Please either BE helpful or just stop posting.

Posting Permissions

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