Howto get and set the current caret position?

S

SvenC

Hi,

in the MVP FAQ I found how to get the current page and can also use the GoTo
method to jump to any page.
But I would like to get the exact current caret position before I close a
document so that later I can set the caret if I open the document again.
Can that be done?
 
S

Shauna Kelly

Hi SvenC

You can get information about the cursor position through
Application.Selection.Range.Information(). Look up VBA's help for all the
parameters that .Information can take and what they return.

However, there's no need to save the information. When you open a Word
document, do Shift-F5. That returns the insertion point to where it was when
you saved the document. You can achieve the same thing in VBA using
Application.GoBack

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
S

SvenC

Hi Shauna,
Hi SvenC

You can get information about the cursor position through
Application.Selection.Range.Information(). Look up VBA's help for all
the parameters that .Information can take and what they return.

However, there's no need to save the information. When you open a Word
document, do Shift-F5. That returns the insertion point to where it
was when you saved the document. You can achieve the same thing in
VBA using Application.GoBack

Neither Shift+F5 nor GoBack is working for me in Word 2007. I tried this
VBA:

sFile = ActiveDocument.FullName
ActiveDocument.Close
Set odoc = Application.Documents.Open(sFile)
Application.GoBack

But the document is opened with the beginning shown, no movement by calling
GoBack or pressing Shift+F5

The Information Method was how I got the current page, but unfortunately it
is not giving the current line, only the line relative to the page.
But when I use Goto, to go to an absolute page and from there to a relative
line the caret is always position on the absolute line from the beginning.

I was able to take the Selection Start and End properties and use
oDoc.Range(start, end).Select to position the insertion mark correctly. Now
only the scrolling position is not as before. I can get the scrolling
postion of the current window in percent but unfortunately the document is
loaded step by step so right after the Documents.Open call the scrolling bar
is not exact as the document and its statistics (number of pages,
characters...) is loaded in the background.

But with the insertion point being correct it is quite OK for me.

Thanks,
SvenC
 

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