Any way to have a messagebox pop up whenever a new page is started?

J

Jean-Guy Marcil

xerj was telling us:
xerj nous racontait que :
Thanks in advance if anyone knows how to do it.


As Jezebel pointed out, you cannot really trap user keystrokes... but, you
can trap user selection changes. So, this might be useful.
Whenever the user clicks somewhere; selects something to delete, copy, paste
or to change paragraph/font attributes; inserts a table, etc., then the
following code will work and if the number of pages changes, you can be
informed:

Paste this in the ThisDocument module: (This bit will call the standard
module that must register the event handler every time the document is
opened or created from the template, if you are working from a template)

'_______________________________________

Private Sub Document_New()



WordEventModule.RegisterEH

PageCount = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)



End Sub

'_______________________________________



'_______________________________________

Private Sub Document_Open()



WordEventModule.RegisterEH

PageCount = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)



End Sub

'_______________________________________



In a class module that you MUST name "WordEventHandler", paste the following
code: (This is the code that actually does the work when the event is
triggered. You could have a call to a procedure in a standard module if you
prefer)



'_______________________________________

Public WithEvents WordApp As Word.Application



'_______________________________________

Private Sub WordApp_WindowSelectionChange(ByVal Sel As Selection)



Dim NewPageCount As Long



NewPageCount = ActiveDocument.Range.Information(wdNumberOfPagesInDocument)

If NewPageCount > PageCount Then

MsgBox "The page count has increased"

PageCount = NewPageCount

ElseIf NewPageCount < PageCount Then

MsgBox "The page count has decreased"

PageCount = NewPageCount

End If



End Sub'_______________________________________



Finally, in a standard module which you MUST name "WordEventModule", paste
this code: (This will register the event handler, or activate it if you
will)



'_______________________________________

Public MyWordEvent As New WordEventHandler

Public PageCount As Long

'_______________________________________

Public Sub RegisterEH()

'Register the Event Handler

Set MyWordEvent.WordApp = Word.Application

End Sub

'_______________________________________

For more on this application-triggered event, see:
http://msdn.microsoft.com/library/d...tml/woevtWindowSelectionChange_HV05277162.asp
(Watch out for URL wraping!)

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

xerj

Wow, thanks JG!

It's kind of frustrating that keystrokes can't be trapped, or there isn't an
inbuilt or way to build something like a "OnNewPageCreated" event. It must
be doing it initernally, so you'd think that when it fires you'd have
programattic access to it somehow.

But thanks for all that code!
 
C

Charles Kenyon

I don't know of any way to do this, but suspect that there is a way to
accomplish what you want with your message box. What is it you are trynig to
accomplish?
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide




--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
X

xerj

Hi Charles.

The message box idea was just to test something.

What I am after is a way to have Word automatically assess a paragraph that
goes over the end of the page and apply formatting as the user types. It's a
little like widow/orphan control but there are some other added wrinkles.

If you have any ideas I'd like to hear them!
 
C

champollion.yves

I do event trapping on keystrokes all the time.

I suppose you're programmers, so I'll only describe the principle at
work, with minimal code.

You associate a common key with a macro. For example, the spacebar. The
necessary code would be as follows:

KeyBindings.Add 1, "MyKeyEventSpace", BuildKeyCode(wdKeySpacebar)

Every time the users presses the space bar, your MyKeyEventSpace macro
fires. Make sure the first thing the macro does is *actually* type the
space:

Selection = " ": Selection.Collapse 0

Inside that macro, you have a persistent variable (by persistent, I
mean a document variable, not a VBA variable that dies at the first
End) that records the current page number. If that number is different
from the current page, you know you moved to a different page and you
can take action.

Now you may also link some navigation keys (KeyDown, PageDown) in the
same way to make sure the macro fires even when you just navigate
without typing.

Make sure the KeyBindings is disabled when not in use anymore.

Cheers,
Yves Champollion
www.champollion.net
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
I do event trapping on keystrokes all the time.

I suppose you're programmers, so I'll only describe the principle at
work, with minimal code.

You associate a common key with a macro. For example, the spacebar.
The necessary code would be as follows:

KeyBindings.Add 1, "MyKeyEventSpace", BuildKeyCode(wdKeySpacebar)

Every time the users presses the space bar, your MyKeyEventSpace macro
fires. Make sure the first thing the macro does is *actually* type the
space:

Selection = " ": Selection.Collapse 0

Inside that macro, you have a persistent variable (by persistent, I
mean a document variable, not a VBA variable that dies at the first
End) that records the current page number. If that number is different
from the current page, you know you moved to a different page and you
can take action.

Now you may also link some navigation keys (KeyDown, PageDown) in the
same way to make sure the macro fires even when you just navigate
without typing.

So, to answer the original post, you would have to bind all possible keys.
including the SHIFT and ALT-CAR combinations)?
Lots of work, no?

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

champollion.yves

xerj said:
What I am after is a way to have Word automatically assess a paragraph that
goes over the end of the page and apply formatting as the user types.
It's a
little like widow/orphan control but there are some other added
wrinkles.

No need to bind all possible keys. IMHO, you don't need to run the test
[have we moved one page?] at *every* keystroke. Trapping the spacebar
is amply sufficient, unless xerj wants the Japanese/Chinese to use his
system - they make less use of spaces indeed.

AFAIU, xerj needs to notice when, in the course of typing, textflow
moves into a new page (do I get him right?). I would trap the Enter key
in addition to the Spacebar, but it's a matter of choice. The less keys
you trap the better. It's pretty unlikely someone would type and type
without spaces.

Cheers,
Yves Champollion
www.champollion.net
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
xerj writes:
What I am after is a way to have Word automatically assess a
paragraph that
goes over the end of the page and apply formatting as the user types.
It's a
little like widow/orphan control but there are some other added
wrinkles.

No need to bind all possible keys. IMHO, you don't need to run the
test [have we moved one page?] at *every* keystroke. Trapping the
spacebar is amply sufficient, unless xerj wants the Japanese/Chinese
to use his system - they make less use of spaces indeed.

AFAIU, xerj needs to notice when, in the course of typing, textflow
moves into a new page (do I get him right?). I would trap the Enter
key in addition to the Spacebar, but it's a matter of choice. The
less keys you trap the better. It's pretty unlikely someone would
type and type without spaces.

True enough. But in many cases the user would get the "new page" routine
after the new page is created (when typing long words that wrap to the next
line in mid-word).

But I guess it is a compromise that could satisfy the OP.

He could implement both our ideas and get pretty close to what he wants
without too much overhead.

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

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