Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start of Line/Paragraph

Status
Not open for further replies.
Jan 28, 2003
149
GB
Hi,

Is there a way of moving to the start of a line or paragraph in VBA? Failing that, anyway of determining your current character position within the current line?

Thanks

B.M.
 
Hi BlueMonkey,

Line is easy - it's the default for the Home Key, so ..

[blue][tt] Selection.HomeKey[/tt][/blue]

.. does the trick. Paragraph is a bit trickier; this will do it ..

[blue][tt] ActiveDocument.Range(Selection.Paragraphs(1).Range.Start, Selection.Paragraphs(1).Range.Start).Select[/tt][/blue]

.. but there should be an easier way.


Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
With the insertion point within a paragraph, this will move the insertion point to the start of that paragraph. Note this is the VBA equivalent to the keyboard shortcut Ctrl-UpArrow, which does the same thing.

Selection.MoveUp Unit:=wdParagraph, Count:=1

.MoveDown will move it to the start of the next paragraph. Obviously you could also do a count greater than 1.

Gerry
 
Hi Gerry,

I knew my statement was a bit long [smile]

A minor point about yours, though; it will go back to the previous paragraph if you're currently at the start of one, which may not be what BlueMonkey wants.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
That is correct, it will.

However, I do not quite understand why you would want to move to the start of a paragraph if you are already there.

In that case, you had better add some conditional testing to see if you ARE at the start of a paragraph. As well as some error trapping to ensure that you are in fact at the start of the paragraph you want. As well as some sort of way of determining what in fact makes it the paragraph you want.

But OK. Enough long windedness. Try this then:

Selection.StartOf Unit:=wdParagraph, Extend:=wdMove

Ha! It works even if you are at the start of the paragraph.

[smile]


Gerry
 
Hi Gerry,

That's it! [wink]
StartOf is good - works for wdLine as well.

(I don't know exactly what's wanted but, perhaps, the exact position isn't known and it may or may not be at the start of a paragraph, or whatever.)

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
...and a whole bunch of others too!

wdCell
wdCharacter
wdColumn
wdParagraph
wdRow
wdSection
wdSentence
wdStory
wdTable
wdWord


I find wdStory particularly useful within Headers/Footers. StartOf Section is handy too. I am not sure how useful having a selection point go to the start of the current character. Although changing wdMove to wdExtend will select the previous character.

Gerry
 
Hi Gerry,

I presume all of the unit moves will be based on the first such unit in the Selection - I haven't checked this out but I guess Selection.StartOf wdCharacter is, thus, the same as Selection.Collapse wdCollapseStart.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks for all your help. Had to condense a file in a bit of a hurry yesterday, so went with the first suggestion - HomeKey, which worked fine.

Reason I asked is the method I was using WAS moving to the previous paragraph in the event of a blank line.

Thanks all for your help.

PS - is there a way of calculating your current position within a line (eg character no 5)?

B.M.
 
Yes. You woud have to tweak this to remove spaces, or other characters (", -, and other stuff). However, this will count the total number of charscters, including spaces.

Code:
Function NumCharacter()

' just to make sure working with single selection point
   Selection.Collapse direction:=wdCollapseStart
' extend selection to startof line
   Selection.StartOf unit:=wdLine, Extend:=wdExtend
' get the length of theselection
   NumCharacter = Selection.End - Selection.Start
' alternative is NumCharacters = Selection.Characters.Count
End Function

Note that this leaves the selection extended to the start of the line. If you want to return back to original location, use Selection.Collapse Direction:=wdCollapseEnd after.

Gerry
 
Hi B.M.,

You could try this ..

Code:
[blue]PositionInLine = Selection.Start - ActiveDocument.Bookmarks("\Line").Start + 1[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top