moving down and to end of paragraph

User avatar
stuck
Panoramic Lounger
Posts: 8176
Joined: 25 Jan 2010, 09:09
Location: retirement

moving down and to end of paragraph

Post by stuck »

At one point in my code this construct does what I expect, it moves the insertion point down into the next paragraph and positions it at the start of that paragraph:

Code: Select all

        Set rng = Selection.Next(Unit:=wdParagraph, Count:=1)
        rng.Collapse Direction:=wdCollapseStart
        rng.Select
Elsewhere in my code I'd like to do something similar but have the insertion point to be at the end of the paragraph. I expected that all I needed to do was use that above snippet but replace 'wdCollapseStart' with 'wdCollapseEnd'. Doing that didn't work. The insertion point ended up at the start of the paragraph after the next paragraph.

Perhaps this is something to do with the fact the 'next' parapgraph, the one I want to end up at the end of, is split into four lines by 'shift enter' (new line) characters?? As yet I've not found an elegant way of getting the insertion point to where I want it (so I can insert a bookmark at that point). Instead I've resorted to the crude but effective use of:

Code: Select all

        Selection.MoveDown
        Selection.MoveDown
        Selection.MoveDown
        Selection.MoveDown
to get the insertion point where I want it.

Any thoughts?

Ken

User avatar
HansV
Administrator
Posts: 78494
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: moving down and to end of paragraph

Post by HansV »

Try this:

Code: Select all

    Selection.Move Unit:=wdParagraph, Count:=2
    Selection.MoveLeft Unit:=wdCharacter
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8176
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: moving down and to end of paragraph

Post by stuck »

I think one of the many tweaks I tried involved 'Count:=2' but NOT in conjunction with then moving left by one character so that looks promising, thanks.

I'll report back in due course...

Ken

User avatar
stuck
Panoramic Lounger
Posts: 8176
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: moving down and to end of paragraph

Post by stuck »

...reporting back in due course...

Yes, that works and after not too much thought I now understand why my expection of 'Direction:=wdCollapseEnd' was flawed. 'wdCollapseEnd' does put the insertion point at the end of the paragraph BUT in VBA a paragraph includes the paragraph mark, meaning the insertion point is placed to right of the paragraph mark, aka the beginning of the next paragraph.

Thanks again. Now all I've got to do is retain what I've learnt here.

Ken