How to select a paragraph via VBA

P

pac

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
 
J

JGM

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!
 
Joined
Sep 8, 2016
Messages
12
Reaction score
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
=================================
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top