Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Dec 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy multiple selection of interrogative sentences in word document

    Hi there;
    I need a code which searches for multiple question form sentences in a word document and selects (not highlights) all of them at once!
    I am a newbie and I would be so grateful for any help
    thanks

  2. #2
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Although selecting non-contiguous text is offered by Word when working interactively, it is not possible to do this with VBA. You can highlight (or apply some other distinctive attribute) to the questions, and then process your highlighted chunks of test.

    Code:
    Sub selectQuestions()
        Dim rng As Range
        Dim s As Range
        For Each s In ActiveDocument.Sentences
            Set rng = s
            rng.MoveEnd wdCharacter, -1
            If rng.Characters.Last = "?" Then
                rng.HighlightColorIndex = wdBlue
            End If
        Next
    End Sub

  3. #3
    New Lounger
    Join Date
    Dec 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dear Richard;
    many thanks for your quick reply!
    as I understand of your explanation, there won't be any way to select non-contiguous text with VBA!
    regarding to the selecting non-contiguous method for selection which word offers, what kind of distinctive attribute could be assigned to the sentences with VBA so that after that one could use it to select all of them at once?
    any suggestion?
    thanks

  4. #4
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    What is it that you want to do with the selected blocks of text? VBA simply cannot select multiple contiguous blocks of text. Have a look at the following MS knowledge base article:

    https://support.microsoft.com/en-us/kb/288424

  5. #5
    New Lounger
    Join Date
    Dec 2015
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dear Richard;
    I just want to apply a style (for example Heading 1) to all of the question form sentences in a document at once.
    cheers

  6. #6
    5 Star Lounger
    Join Date
    Mar 2001
    Location
    New York, NY
    Posts
    922
    Thanks
    2
    Thanked 12 Times in 11 Posts
    Then using the code I sent previously, apply the style instead of the highlight. Of course you'll want to be sure that QuestionStyle (or whatever style name you use) exists in the document, otherwise you'll get an error. You can trap the error with code, but if you're the sole user, you might not need to.

    Code:
    Sub selectQuestions()
        Dim rng As Range
        Dim s As Range
        For Each s In ActiveDocument.Sentences
            Set rng = s
            rng.MoveEnd wdCharacter, -1
            If rng.Characters.Last = "?" Then
                rng.Style = "QuestionStyle"
            End If
        Next
    End Sub

Posting Permissions

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