Word macro - maintaining scroll position

B

BenCh

Hello!

I've recently developed my first macro for Microsoft Word 2007 with the help
of some of the people on this forum.

I'm very pleased with it, but it has a minor bug. [Or do we call them
'design features' here? ;o) ]

It runs immediately before the document is saved or printed. By the time
it's finished going through the actions I ask it to do, the user has lost his
place in the document. If he was reading or editing the bottom of page 3, the
macro will take him to the top of the penultimate page, because the macro
does some work down there. This means the user has to scroll back up the
document to find his place after it has run.

How can I change my macro so that it either:
1) Remembers where the user was looking and takes him straight back to that
position at the end of the macro, so it doesn't appear to have scrolled away,
or...
2) Run the macro so it executes its statements without appearing to have
moved the position of the document.

Any advice is much appreciated, thank you.
 
G

Graham Mayor

One way would be to mark the cursor position before running the code then
return to that position after running it eg

Dim oDoc as Document
Set oDoc = ActiveDocument
oDoc.Bookmarks.Add Range:=Selection.Range, Name:="bLast"
'Do your stuff
oDoc.Bookmarks("bLast").Select


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Gordon Bentley-Mix

Another way is to modify your macro so you don't have to move the selection
(cursor). Look at using the Range object instead of the Selection object in
your code. Pretty much anything you do with the Selection object you can do
with the Range object. You just have to work out how to identify the Range
you want to work with, which really just requires a slightly different view
of how a document is constructed. Developing such an understanding will
certainly benefit you in the long run.

Of course Graham's approach is simple and effective, and it will do the job
for this particular instance - although you may want to delete the bookmark
afterwards using:

oDoc.Bookmarks("bLast").Delete

--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!

Graham Mayor said:
One way would be to mark the cursor position before running the code then
return to that position after running it eg

Dim oDoc as Document
Set oDoc = ActiveDocument
oDoc.Bookmarks.Add Range:=Selection.Range, Name:="bLast"
'Do your stuff
oDoc.Bookmarks("bLast").Select


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hello!

I've recently developed my first macro for Microsoft Word 2007 with
the help of some of the people on this forum.

I'm very pleased with it, but it has a minor bug. [Or do we call them
'design features' here? ;o) ]

It runs immediately before the document is saved or printed. By the
time it's finished going through the actions I ask it to do, the user
has lost his place in the document. If he was reading or editing the
bottom of page 3, the macro will take him to the top of the
penultimate page, because the macro does some work down there. This
means the user has to scroll back up the document to find his place
after it has run.

How can I change my macro so that it either:
1) Remembers where the user was looking and takes him straight back
to that position at the end of the macro, so it doesn't appear to
have scrolled away, or...
2) Run the macro so it executes its statements without appearing to
have moved the position of the document.

Any advice is much appreciated, thank you.
 
J

Jay Freedman

Hi Gordon,

This is good advice in general. But for the specific macro BenCh developed
based on the information in the thread "Count words in certain sections", it
isn't possible. The Word Count dialog's Execute method returns the word
count for the Selection (if the Selection isn't collapsed), and that usage
can't be replaced with a Range object. There are a few Selection-only things
in VBA -- most unfortunate.

The bookmark workaround is the only choice here.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
Another way is to modify your macro so you don't have to move the
selection (cursor). Look at using the Range object instead of the
Selection object in your code. Pretty much anything you do with the
Selection object you can do with the Range object. You just have to
work out how to identify the Range you want to work with, which
really just requires a slightly different view of how a document is
constructed. Developing such an understanding will certainly benefit
you in the long run.
Of course Graham's approach is simple and effective, and it will do
the job for this particular instance - although you may want to
delete the bookmark afterwards using:

oDoc.Bookmarks("bLast").Delete


Graham Mayor said:
One way would be to mark the cursor position before running the code
then return to that position after running it eg

Dim oDoc as Document
Set oDoc = ActiveDocument
oDoc.Bookmarks.Add Range:=Selection.Range, Name:="bLast"
'Do your stuff
oDoc.Bookmarks("bLast").Select


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Hello!

I've recently developed my first macro for Microsoft Word 2007 with
the help of some of the people on this forum.

I'm very pleased with it, but it has a minor bug. [Or do we call
them 'design features' here? ;o) ]

It runs immediately before the document is saved or printed. By the
time it's finished going through the actions I ask it to do, the
user has lost his place in the document. If he was reading or
editing the bottom of page 3, the macro will take him to the top of
the penultimate page, because the macro does some work down there.
This means the user has to scroll back up the document to find his
place after it has run.

How can I change my macro so that it either:
1) Remembers where the user was looking and takes him straight back
to that position at the end of the macro, so it doesn't appear to
have scrolled away, or...
2) Run the macro so it executes its statements without appearing to
have moved the position of the document.

Any advice is much appreciated, thank you.
 
B

BenCh

Thanks for your continued support on this, Jay.

I'm very pleased with the result I got from the bookmark solution. It's not
as neat as a Range solution would have been (if it were possible) but does
exactly what I want.

My friends here and at other universities are queuing up to use my solution!
It seems there is a real need to have a field like this built into MS Word.

Best wishes
 
B

BenCh

Does MS Word raise any events or anything at the point immediately after
macros are enabled by the user?

I'd like to run a sub routine on Document_New and Document_Open but if
that's not possible (owing to macros being disabled) can I run it as soon as
they're enabled?
 
G

Gordon Bentley-Mix

Thanks for the clarification Jay. I wasn't aware of the other thread, and I
fully understand why using the Selection object is the only option here.

I saw the thread in the private NG about the Selection-only limitations, and
obviously this is one of them. Besides, I can't imagine how you would define
a Range object for doing a word count like BenCh wants...
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!

Jay Freedman said:
Hi Gordon,

This is good advice in general. But for the specific macro BenCh developed
based on the information in the thread "Count words in certain sections",
it isn't possible. The Word Count dialog's Execute method returns the word
count for the Selection (if the Selection isn't collapsed), and that usage
can't be replaced with a Range object. There are a few Selection-only
things in VBA -- most unfortunate.

The bookmark workaround is the only choice here.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
Another way is to modify your macro so you don't have to move the
selection (cursor). Look at using the Range object instead of the
Selection object in your code. Pretty much anything you do with the
Selection object you can do with the Range object. You just have to
work out how to identify the Range you want to work with, which
really just requires a slightly different view of how a document is
constructed. Developing such an understanding will certainly benefit
you in the long run.
Of course Graham's approach is simple and effective, and it will do
the job for this particular instance - although you may want to
delete the bookmark afterwards using:

oDoc.Bookmarks("bLast").Delete


Graham Mayor said:
One way would be to mark the cursor position before running the code
then return to that position after running it eg

Dim oDoc as Document
Set oDoc = ActiveDocument
oDoc.Bookmarks.Add Range:=Selection.Range, Name:="bLast"
'Do your stuff
oDoc.Bookmarks("bLast").Select


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

BenCh wrote:
Hello!

I've recently developed my first macro for Microsoft Word 2007 with
the help of some of the people on this forum.

I'm very pleased with it, but it has a minor bug. [Or do we call
them 'design features' here? ;o) ]

It runs immediately before the document is saved or printed. By the
time it's finished going through the actions I ask it to do, the
user has lost his place in the document. If he was reading or
editing the bottom of page 3, the macro will take him to the top of
the penultimate page, because the macro does some work down there.
This means the user has to scroll back up the document to find his
place after it has run.

How can I change my macro so that it either:
1) Remembers where the user was looking and takes him straight back
to that position at the end of the macro, so it doesn't appear to
have scrolled away, or...
2) Run the macro so it executes its statements without appearing to
have moved the position of the document.

Any advice is much appreciated, thank you.
 
J

Jay Freedman

Does MS Word raise any events or anything at the point immediately after
macros are enabled by the user?

I'd like to run a sub routine on Document_New and Document_Open but if
that's not possible (owing to macros being disabled) can I run it as soon as
they're enabled?

No, 'fraid not.

You can provide a toolbar button (in 2007, a Quick Access Toolbar button) to run
a macro manually after macros are enabled. However, Word won't let you add a
button that runs Document_New or Document_Open, or anything else in the
ThisDocument module. The macro that the button runs has to be in a regular
module.

You have two options:

- Create an AutoNew procedure in a regular module, move your code from
Document_New into AutoNew, and remove Document_New. Also move code from
Document_Open into AutoOpen. Then you can create buttons to run AutoNew and/or
AutoOpen in case macros were disabled and prevented the Auto macros from
running.

- Leave the code in Document_New. Create a macro in a regular module with some
name other than AutoNew, so it never runs automatically, and put into it the
single line
Call Document_New
Give that macro a toolbar button for use if Document_New didn't run
automatically. Repeat for Document_Open.
 

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