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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adi
    Replies:
    3
    Views:
    278
    Jezebel
    Nov 2, 2004
  2. Robert Mark Bram

    Select current paragraph

    Robert Mark Bram, Apr 3, 2005, in forum: Word VBA
    Replies:
    1
    Views:
    207
    Robert Mark Bram
    Apr 3, 2005
  3. john smith
    Replies:
    1
    Views:
    316
    Greg Maxey
    Aug 23, 2006
  4. andreas
    Replies:
    2
    Views:
    265
    andreas
    Feb 26, 2007
  5. justagrunt
    Replies:
    4
    Views:
    216
    justagrunt
    Apr 6, 2007
  6. CjimO
    Replies:
    4
    Views:
    684
    CjimO
    Dec 18, 2007
  7. andreas
    Replies:
    4
    Views:
    250
    andreas
    Sep 4, 2008
  8. Sardonic
    Replies:
    3
    Views:
    223
    Doug Robbins - Word MVP
    Nov 27, 2008
Loading...