Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Vail, Colorado, USA
    Posts
    201
    Thanks
    38
    Thanked 0 Times in 0 Posts

    VBA to select a block of text

    I apologize I do not have any code to post I have tried recording a macro to no avail. I am trying to document some code I have for Microsoft Access project in Word. I need to be able to manipulate blocks of text.

    What I would like to do starting at the beginning of the document is select say "^pIf though to End If" , there are other blocks " For Next " etc. I will then manipulate those blocks indent format etc. Sounds simple when I write it but have had not luck looking on the web.

    Thanks for any suggestions.

    Peter

  2. #2
    2 Star Lounger Graham Mayor's Avatar
    Join Date
    Mar 2016
    Location
    Cyprus
    Posts
    108
    Thanks
    0
    Thanked 39 Times in 37 Posts
    The following will search a block of text that is a VBA code listing for If ... End If

    However IF may not have a corresponding End If statement, so the following also caters for that.

    It does not cater for nested If ... End If statements.

    Without knowing the exact piece of code you are searching it is difficult to establish a protocol that would cater for what you want to locate.

    Code:
    Sub Macro1()
    Dim oRng As Range, oEnd As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:="If ", MatchCase:=True)
                Set oEnd = oRng.Paragraphs(1).Range
                oEnd.End = oEnd.End - 1
                Do While oEnd.Characters.Last = Chr(32)
                    oEnd.End = oEnd.End - 1
                Loop
                If oEnd.Words.Last = "Then" Then
                    oRng.End = ActiveDocument.Range.End
                    oRng.End = oRng.Start + InStr(oRng, "End ") + 5
                Else
                    oRng.End = oEnd.End
                End If
                'do something with orng
                oRng.Select
                MsgBox oRng.Text
                oRng.Collapse 0
            Loop
        End With
    End Sub
    Graham Mayor - Word MVP
    http://www.gmayor.com

  3. The Following User Says Thank You to Graham Mayor For This Useful Post:

    mitchbvi (2017-08-12)

  4. #3
    3 Star Lounger
    Join Date
    Apr 2003
    Location
    Vail, Colorado, USA
    Posts
    201
    Thanks
    38
    Thanked 0 Times in 0 Posts
    First of all thank you so much for taking the time and trouble to respond. I had managed using the web to put something together that works sort of, but not as elegant as your solution. I had not managed to figure out how to use "Do While" so used a For Next loop and had to reset the active range. Anyway do not know if you have any ideas but I do have nested if--End statements also I have put comments in some instances at the end of the If..... Then line. The word document with the code I am documenting is 91 pages so slow do everything by hand. I have built it over many years and I know some of it is clumsy and in cases just unnecessary and I thought if I documented it properly I could fix. Should be able to fix the comment issue but stumped on the nesting.

    Just in case you can help further I have included my code more to show what I am doing than the selection process.

    Code:
    Sub FindaTextRange()
    'Select's the text between two strings
    Dim rng1 As Range
    Dim rng2 As Range
    Dim strTheText As String
    Dim No_ As Integer
    First = InputBox("Enter the 1st String, remember it is case sensitive")
    Last = InputBox("Enter the 2nd String, remember it is case sensitive")
    Selection.HomeKey unit:=wdStory
    'Setup number of loops
    '###################
    It_ = Split(ActiveDocument.Range, Last) 'Counts Number of the last selection in document to provide a basis for the loops
    No_ = UBound(It_)
    Set rng1 = ActiveDocument.Range 'Very 1st Position in Document
    For x = 1 To No_
        'Set rng1 = ActiveDocument.Range
        If rng1.Find.Execute(FindText:=First) Then
            Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
            If rng2.Find.Execute(FindText:=Last) Then
                strTheText = ActiveDocument.Range(rng1.Start, rng2.End).Text
                ActiveDocument.Range(rng1.Start, rng2.End).Select
                With Selection.ParagraphFormat
                    .LeftIndent = InchesToPoints(0.75)
                End With
                With Selection
                    .Font.Bold = True
                End With
            End If
        End If
        'Resets Range Area from End of last Selection to end of document
        Set rng1 = ActiveDocument.Range(rng2.End, ActiveDocument.Range.End)
        Next x
    End Sub

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
  •