none
Searching within ranges RRS feed

  • Question

  • Hi,

    I want find all range between two character example "[" and "]" but also I want to known when some one of this character dont have partner

    I'm using the following code:

    Private Function FindCharacterInRange(ByVal Character As String, ByVal rango As Word.Range, Optional ByVal Prefix As Boolean = True, Optional ByVal Forward As Boolean = True) As Word.Range()

            Dim rng As Word.Range

            Dim RngResult(2) As Word.Range

            Dim _find As Boolean = True

            Dim _findFin As Boolean = True

     

            rng = Globals.ThisAddIn.Application.ActiveDocument.Range

            rng.Start = rango.Start

            rng.End = rango.End

     

            Select Case Prefix

                Case True

                    With rng.Find

                        .MatchPrefix = True

                        .Forward = Forward

                        _find = .Execute(Character, MatchCase:=True)

                    End With

     

                Case False

                    With rng.Find

                        .MatchSuffix = True

                        .Forward = Forward

                        _find = .Execute(Character, MatchCase:=True)

                    End With

            End Select

     

            If _find Then

     

                RngResult(0) = rng

                If rng.Tables.Count > 0 Then

                    RngResult(1) = rng.Tables(1).Range

                Else

                    RngResult(1) = rng.Paragraphs(1).Range

                End If

                Return RngResult

            Else

                Return Nothing

            End If

        End Function

     

        Private Function FindBookmarkInRange(ByVal Range As Word.Range) As Word.Range

            Dim ArrRNGStart() As Word.Range

            Dim arrRNGEnd() As Word.Range

            Dim WordRng As Word.Range

     

            WordRng = Range

            ArrRNGStart = FindCharacterInRange("]", Range, True, True)

     

            If ArrRNGStart Is Nothing Then

                Return Nothing

            End If

            WordRng.Start = ArrRNGStart(0).Start

            WordRng.End = ArrRNGStart(1).End

            arrRNGEnd = FindCharacterInRange("[", WordRng, False, True)

            If arrRNGEnd Is Nothing Then

                Return Nothing

            End If

            WordRng.Start = ArrRNGStart(0).Start

            WordRng.End = arrRNGEnd(0).End

            Return WordRng

        End Function

     

    but this dont lets me know when there is a character to close "]" without having character to open "[", because ever find forward firt open and later close.

    there any way to search with regular expressions or a way of know when one character exist in a range.??

     

    thanks in advance

    Thursday, December 16, 2010 10:27 PM

Answers

  • Use the Instr() function.  If it returns 0, the character does not appear in the String (Text attribute of the Range.  If it returns anything else, it will be the position of the character in the range

    -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "RegieJose" wrote in message news:4199e4d2-520b-4271-b657-fd44f5d9e536@communitybridge.codeplex.com...

    Hi,

    I want find all range between two character example "[" and "]" but also I want to known when some one of this character dont have partner

    I'm using the following code:

    Private Function FindCharacterInRange(ByVal Character As String, ByVal rango As Word.Range, Optional ByVal Prefix As Boolean = True, Optional ByVal Forward As Boolean = True) As Word.Range()

            Dim rng As Word.Range

            Dim RngResult(2) As Word.Range

            Dim _find As Boolean = True

            Dim _findFin As Boolean = True

            rng = Globals.ThisAddIn.Application.ActiveDocument.Range

            rng.Start = rango.Start

            rng.End = rango.End

            Select Case Prefix

                Case True

                    With rng.Find

                        .MatchPrefix = True

                        .Forward = Forward

                        _find = .Execute(Character, MatchCase:=True)

                    End With

                Case False

                    With rng.Find

                        .MatchSuffix = True

                        .Forward = Forward

                        _find = .Execute(Character, MatchCase:=True)

                    End With

            End Select

            If _find Then

                RngResult(0) = rng

                If rng.Tables.Count > 0 Then

                    RngResult(1) = rng.Tables(1).Range

                Else

                    RngResult(1) = rng.Paragraphs(1).Range

                End If

                Return RngResult

            Else

                Return Nothing

            End If

        End Function

        Private Function FindBookmarkInRange(ByVal Range As Word.Range) As Word.Range

            Dim ArrRNGStart() As Word.Range

            Dim arrRNGEnd() As Word.Range

            Dim WordRng As Word.Range

            WordRng = Range

            ArrRNGStart = FindCharacterInRange("]", Range, True, True)

            If ArrRNGStart Is Nothing Then

                Return Nothing

            End If

            WordRng.Start = ArrRNGStart(0).Start

            WordRng.End = ArrRNGStart(1).End

            arrRNGEnd = FindCharacterInRange("[", WordRng, False, True)

            If arrRNGEnd Is Nothing Then

                Return Nothing

            End If

            WordRng.Start = ArrRNGStart(0).Start

            WordRng.End = arrRNGEnd(0).End

            Return WordRng

        End Function

    but this dont lets me know when there is a character to close "]" without having character to open "[", because ever find forward firt open and later close.

    there any way to search with regular expressions or a way of know when one character exist in a range.??

    thanks in advance


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Friday, December 24, 2010 12:02 PM
    Friday, December 17, 2010 1:52 AM