Detect Cursor at Start of Paragraph

T

Thomas M.

Word 2003

I have the following simple macro.


Sub GreenFont()

If Selection.Start = Selection.End Then
Selection.MoveUp Unit:=wdParagraph, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Font.Color = wdColorGreen
Selection.MoveUp Unit:=wdParagraph, Count:=1
Else
Selection.Font.Color = wdColorGreen
End If

End Sub


If text is selected in the document the entire selection is turned to green
text. Otherwise, the current paragraph is turned to green text. This works
well with one exception--if the insertion point is at the start of the
paragraph it will act on the *previous* paragraph. What is the easiest way
to make it always act on the current paragraph--without regard to the
position of the insertion point--when text is not selected?

--Tom
 
J

Jean-Guy Marcil

Thomas M. was telling us:
Thomas M. nous racontait que :
Word 2003

I have the following simple macro.


Sub GreenFont()

If Selection.Start = Selection.End Then
Selection.MoveUp Unit:=wdParagraph, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend Selection.Font.Color = wdColorGreen
Selection.MoveUp Unit:=wdParagraph, Count:=1
Else
Selection.Font.Color = wdColorGreen
End If

End Sub


If text is selected in the document the entire selection is turned to
green text. Otherwise, the current paragraph is turned to green
text. This works well with one exception--if the insertion point is
at the start of the paragraph it will act on the *previous*
paragraph. What is the easiest way to make it always act on the
current paragraph--without regard to the position of the insertion
point--when text is not selected?

Do not use the Selection object...as you have discovered it is unreliable
and slower as well.
Try this (Which uses the Range property of the selection object to get at
the paragraph):

Sub GreenFont()

With Selection
If .Start = .End Then
.Paragraphs(1).Range.Font.Color = wdColorGreen
Else
.Font.Color = wdColorGreen
End If
End With

End Sub


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

karitaat

Thomas,

fastes way is this:

Sub myGreen()

If Selection.Type = wdSelectionIP Then
'nothing selected: take the paragraph
ActiveDocument.Bookmarks("\Para").Range.Font.Color = wdColorGreen
Else
Selection.Font.Color = wdColorGreen
End If

End Sub

It tests if the Selection is the InsertionPoint (=nothing selected)
If so: it uses a predefined bookmark ("\Para") to select the whole
paragraph (including its ending point! Do you want that?)

regards,
peter


Thomas M. schreef:
 

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