How to select a paragraph via VBA

Discussion in 'Word VBA' started by pac, Dec 3, 2003.

  1. pac

    pac Guest

    I want to select a paragraph in my macro
    I have this type of line
    U:\totttot\toto\t
    tu\laposte.xl

    At present, I search the extension .xls, then I can select the line. (result : tu\laposte.xls
    How can I select the entire paragraph
    U:\totttot\toto\t
    tu\laposte.xl

    Macro use
    Selection.MoveRight Unit:=wdCharacter, Count:=
    Selection.HomeKey Unit:=wdLine, Extend:=wdExten
     
    pac, Dec 3, 2003
    #1
    1. Advertisements

  2. Hi Pac

    Selection.Paragraphs(1).Range.Select

    --
    Regards
    Jonathan West - Word MVP
    http://www.multilinker.com
    Please reply to the newsgroup


    "pac" <> wrote in message
    news:...
    > I want to select a paragraph in my macro.
    > I have this type of line.
    > U:\totttot\toto\ti
    > tu\laposte.xls
    >
    > At present, I search the extension .xls, then I can select the line.

    (result : tu\laposte.xls)
    > How can I select the entire paragraph
    > U:\totttot\toto\ti
    > tu\laposte.xls
    >
    > Macro used
    > Selection.MoveRight Unit:=wdCharacter, Count:=1
    > Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    >
    >
    >
     
    Jonathan West, Dec 3, 2003
    #2
    1. Advertisements

  3. pac

    JGM Guest

    Hi pac,

    To select the current paragraph:

    Selection.Paragraphs(1).Range.Select

    But this will inlcude the paragraph mark, if you do not want the paragraph
    mark,
    add the following line right after the first one:

    Selection.MoveLeft Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend

    But, even better still, work with the range object:

    Dim myRange As Range

    Set myRange = Selection.Paragraphs(1).Range
    'to remove the paragrpah mark if necessary:
    myRange.SetRange myRange.Start, myRange.End - 1

    Now, use myRange to copy, delete, etc. As in:

    myRange.Copy
    myRange.Delete

    Or, if you really need to select it:

    myRange.Select

    HTH
    Cheers!
    --
    _______________________________________
    Jean-Guy Marcil


    "pac" <> a écrit dans le message de news:
    ...
    > I want to select a paragraph in my macro.
    > I have this type of line.
    > U:\totttot\toto\ti
    > tu\laposte.xls
    >
    > At present, I search the extension .xls, then I can select the line.

    (result : tu\laposte.xls)
    > How can I select the entire paragraph
    > U:\totttot\toto\ti
    > tu\laposte.xls
    >
    > Macro used
    > Selection.MoveRight Unit:=wdCharacter, Count:=1
    > Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
    >
    >
    >
     
    JGM, Dec 3, 2003
    #3
  4. pac

    ChrisOK

    Joined:
    Sep 8, 2016
    Messages:
    7
    Likes Received:
    1
    I was looking for the same solution and thought I'd share what worked great for me. Enter the VBA side of your Word doc (Alt + F11), Insert a new Module, Paste in this code, find the quote marks and enter whatever key word(s) you wish, hit the RUN arrow and watch it highlight all paragraphs that contain that key word. I'm very pleased with how it has helped me! Hope it also helps you and others!
    ==================================
    Sub Highlight_Paragraph()

    'THIS CODE HIGHLIGHTS TO THE END OF THE PARAGRAPH WHERE THE TARGET WORD
    'IS FOUND WITHIN THE DOCUMENT

    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
    Do While .Execute(FindText:="Contractor Shall")
    oRng.Paragraphs(1).Range.HighlightColorIndex = wdYellow
    oRng.Collapse 0
    Loop
    End With
    lbl_Exit:
    Set oRng = Nothing
    Exit Sub
    End Sub
    =================================
     
    ChrisOK, Sep 9, 2016
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.