Selecting a paragraph in VBA

R

Roderick O'Regan

I can see in VBA Help how to select the first paragraph in a document. Or
the fourth, or whatever (Using .Range). But, please, how does one program
VBA to select any paragraph that the cursor is sitting in?

My end result is to be able to apply a formatting property to it which I
know how to do.

Regards

Roderick O'Regan
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Roderick O'Regan > écrivait :
In this message, < Roderick O'Regan > wrote:

|| I can see in VBA Help how to select the first paragraph in a document. Or
|| the fourth, or whatever (Using .Range). But, please, how does one program
|| VBA to select any paragraph that the cursor is sitting in?
||
|| My end result is to be able to apply a formatting property to it which I
|| know how to do.
||

Try, for example, something like:

Selection.Paragraphs(1).Range.Font.Bold = True

etc.

You always use that to select whatever object is connected with the cursor,

as in:
Selection.Tables(1).Borders...
Selection.Sections(1).Range...

Of course, you sometimes have to test first if it is a valid selection.
E.g., you want to check that the cursor is in a table before using
Selection.Tables(1).Borders with a test like

'_______________________________________
If Selection.Information(wdWithInTable) Then
MsgBox "The cursor is in a table"
Else
MsgBox "The cursor is NOT in a table"
End If
'_______________________________________

Good luck.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jay Freedman

Hi Roderick

This can be hard to get your head around, but the syntax you want is

Selection.Paragraphs(1).Range.Select

Read this right-to-left, it says "select the range of the first
paragraph that occurs within the current selection". If the current
selection is collapsed to a single point or covers less than a whole
paragraph, the word "occurs" means "has any part within".

The part that can be hard to remember is that Selection.Paragraphs(1)
is *not* the same as ActiveDocument.Paragraphs(1) unless the selection
happens to be in the first paragraph of the document.
 
R

Roderick O'Regan

Thanks to both yourself and Jean-Guy above has done the trick. Thanks also
for the short "tute" Jay. DOH! It all makes sense now.

Regards

Roderick
 

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