I am developing an MS Word (xp) template for a corporate letterhead that will
automatically insert the reference number (already stored in a variable from
the first page) into the header of the second (and subsequent) pages.

I am aware that there is no "new page" event in Word that can be caught.

The solution I am working on involves checking the
'ActiveDocument.BuiltInDocumentProperties("number of pages")' property and
running the code as soon as the page number has incremented. Unfortunately, I
cannot find a way of checking this value each time the user types a new
character.

Here is the code I am using at this time :

Option Explicit
Private WithEvents wdApp As Word.Application
Dim page1 As Integer
Dim page2 As Integer

'This line is executed during the loading of the document
page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")


Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
If page2 > page1 Then
MsgBox "Now on page " & Format(page2)
'Do the necessary operation here...
.
.
.
End If
End Sub

Unfortunately, the above sub is only works when I click with the mouse
anywhere in the document.

I tried other subs from the same set and I thought that
'wdApp_DocumentChange()' would be the one to use, but this dosen't work
either.

Can anyone give me a pointer as to how I can trap the 'new character event'
(if there is one) and to check if the page number has changed ?

Any help will be greatly appreciated !

Many thanks,
--
Lars

Re: Catching the New Page event in VBA by Tony

Tony
Wed Jul 16 05:44:14 PDT 2008

The Window Selection_Change event is the closest you will get (without using
heaps of APIs) - and it has some side effects in the UI, but ...

What is it you want to do when a new page is started?

--
Enjoy,
Tony

"Lars" <Lars@discussions.microsoft.com> wrote in message
news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
>I am developing an MS Word (xp) template for a corporate letterhead that
>will
> automatically insert the reference number (already stored in a variable
> from
> the first page) into the header of the second (and subsequent) pages.
>
> I am aware that there is no "new page" event in Word that can be caught.
>
> The solution I am working on involves checking the
> 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property and
> running the code as soon as the page number has incremented.
> Unfortunately, I
> cannot find a way of checking this value each time the user types a new
> character.
>
> Here is the code I am using at this time :
>
> Option Explicit
> Private WithEvents wdApp As Word.Application
> Dim page1 As Integer
> Dim page2 As Integer
>
> 'This line is executed during the loading of the document
> page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>
>
> Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> If page2 > page1 Then
> MsgBox "Now on page " & Format(page2)
> 'Do the necessary operation here...
> .
> .
> .
> End If
> End Sub
>
> Unfortunately, the above sub is only works when I click with the mouse
> anywhere in the document.
>
> I tried other subs from the same set and I thought that
> 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
> either.
>
> Can anyone give me a pointer as to how I can trap the 'new character
> event'
> (if there is one) and to check if the page number has changed ?
>
> Any help will be greatly appreciated !
>
> Many thanks,
> --
> Lars


Re: Catching the New Page event in VBA by macropod

macropod
Wed Jul 16 05:46:02 PDT 2008

Hi Lars,

If the paragraph with your reference number is formatted in a Style that isn't used anywhere else in the document, then you could
use a STYLEREF field to replicate it in the header.

Suppressing the STYLEREF field display on the 1st page is especially easy to manage if the document template is set up with a
'different first page' under File|Page Setup|Layout and you put the STYLEREF field into the template's second page header. No vba
required.

--
Cheers
macropod
[MVP - Microsoft Word]


"Lars" <Lars@discussions.microsoft.com> wrote in message news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
>I am developing an MS Word (xp) template for a corporate letterhead that will
> automatically insert the reference number (already stored in a variable from
> the first page) into the header of the second (and subsequent) pages.
>
> I am aware that there is no "new page" event in Word that can be caught.
>
> The solution I am working on involves checking the
> 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property and
> running the code as soon as the page number has incremented. Unfortunately, I
> cannot find a way of checking this value each time the user types a new
> character.
>
> Here is the code I am using at this time :
>
> Option Explicit
> Private WithEvents wdApp As Word.Application
> Dim page1 As Integer
> Dim page2 As Integer
>
> 'This line is executed during the loading of the document
> page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>
>
> Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> If page2 > page1 Then
> MsgBox "Now on page " & Format(page2)
> 'Do the necessary operation here...
> .
> .
> .
> End If
> End Sub
>
> Unfortunately, the above sub is only works when I click with the mouse
> anywhere in the document.
>
> I tried other subs from the same set and I thought that
> 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
> either.
>
> Can anyone give me a pointer as to how I can trap the 'new character event'
> (if there is one) and to check if the page number has changed ?
>
> Any help will be greatly appreciated !
>
> Many thanks,
> --
> Lars


Re: Catching the New Page event in VBA by Lars

Lars
Wed Jul 16 09:10:06 PDT 2008

Thanks, Tony, for your prompt reply. As you say, it is wise to avoid Windows
API calls, as it gets complicated. In answer to your question, when a user
creates a new document based on this template, a dialog box opens
automatically and the user types in the address and all other info using
textboxes and combo boxes. The reference number and the subject of the letter
are also entered here (and stored in global string variables). The OK button
then inserts the data (with all required paragraph formatting, font selection
etc.) such that the user can immediately start typing the body of the letter.
The address is positioned on the page so that it fits in the envelope window,
and all the other details (date, senders address, signature etc.) are
automatically positioned on the page.

If the letter is long, and the user types into a second (or even a third)
page then, at the moment the new page is created, the reference code and
subject of the letter must be inserted into the new page header, using the
previously stored data (I use ranges and frames for this part). Life would be
SO much easier if a New Page event existed ! Thanks again, Tony for your help
:)
--
Lars


"Tony Jollans" wrote:

> The Window Selection_Change event is the closest you will get (without using
> heaps of APIs) - and it has some side effects in the UI, but ...
>
> What is it you want to do when a new page is started?
>
> --
> Enjoy,
> Tony
>
> "Lars" <Lars@discussions.microsoft.com> wrote in message
> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
> >I am developing an MS Word (xp) template for a corporate letterhead that
> >will
> > automatically insert the reference number (already stored in a variable
> > from
> > the first page) into the header of the second (and subsequent) pages.
> >
> > I am aware that there is no "new page" event in Word that can be caught.
> >
> > The solution I am working on involves checking the
> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property and
> > running the code as soon as the page number has incremented.
> > Unfortunately, I
> > cannot find a way of checking this value each time the user types a new
> > character.
> >
> > Here is the code I am using at this time :
> >
> > Option Explicit
> > Private WithEvents wdApp As Word.Application
> > Dim page1 As Integer
> > Dim page2 As Integer
> >
> > 'This line is executed during the loading of the document
> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >
> >
> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> > If page2 > page1 Then
> > MsgBox "Now on page " & Format(page2)
> > 'Do the necessary operation here...
> > .
> > .
> > .
> > End If
> > End Sub
> >
> > Unfortunately, the above sub is only works when I click with the mouse
> > anywhere in the document.
> >
> > I tried other subs from the same set and I thought that
> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
> > either.
> >
> > Can anyone give me a pointer as to how I can trap the 'new character
> > event'
> > (if there is one) and to check if the page number has changed ?
> >
> > Any help will be greatly appreciated !
> >
> > Many thanks,
> > --
> > Lars
>
>

Re: Catching the New Page event in VBA by Tony

Tony
Wed Jul 16 09:25:57 PDT 2008

You have all the information you need to set up the second page header right
at the beginning - you don't need to wait and see whether or not it is
needed.

When you do all the other setup stuff, add some code like this:

With ActiveDocument.Sections(1)
.PageSetup.DifferentFirstPageHeaderFooter = True
.Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever on
page 1"
.Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on page
2 onwards"
End With

Excatly what you set up is up to you, of course :-)

When, or if, the user starts a new page the different header will be used
automatically. If they don't start a new page, they'll never see it. No need
for any event processing.

--
Enjoy,
Tony

"Lars" <Lars@discussions.microsoft.com> wrote in message
news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
> Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
> Windows
> API calls, as it gets complicated. In answer to your question, when a user
> creates a new document based on this template, a dialog box opens
> automatically and the user types in the address and all other info using
> textboxes and combo boxes. The reference number and the subject of the
> letter
> are also entered here (and stored in global string variables). The OK
> button
> then inserts the data (with all required paragraph formatting, font
> selection
> etc.) such that the user can immediately start typing the body of the
> letter.
> The address is positioned on the page so that it fits in the envelope
> window,
> and all the other details (date, senders address, signature etc.) are
> automatically positioned on the page.
>
> If the letter is long, and the user types into a second (or even a third)
> page then, at the moment the new page is created, the reference code and
> subject of the letter must be inserted into the new page header, using the
> previously stored data (I use ranges and frames for this part). Life would
> be
> SO much easier if a New Page event existed ! Thanks again, Tony for your
> help
> :)
> --
> Lars
>
>
> "Tony Jollans" wrote:
>
>> The Window Selection_Change event is the closest you will get (without
>> using
>> heaps of APIs) - and it has some side effects in the UI, but ...
>>
>> What is it you want to do when a new page is started?
>>
>> --
>> Enjoy,
>> Tony
>>
>> "Lars" <Lars@discussions.microsoft.com> wrote in message
>> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
>> >I am developing an MS Word (xp) template for a corporate letterhead that
>> >will
>> > automatically insert the reference number (already stored in a variable
>> > from
>> > the first page) into the header of the second (and subsequent) pages.
>> >
>> > I am aware that there is no "new page" event in Word that can be
>> > caught.
>> >
>> > The solution I am working on involves checking the
>> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property
>> > and
>> > running the code as soon as the page number has incremented.
>> > Unfortunately, I
>> > cannot find a way of checking this value each time the user types a new
>> > character.
>> >
>> > Here is the code I am using at this time :
>> >
>> > Option Explicit
>> > Private WithEvents wdApp As Word.Application
>> > Dim page1 As Integer
>> > Dim page2 As Integer
>> >
>> > 'This line is executed during the loading of the document
>> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>> >
>> >
>> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
>> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>> > If page2 > page1 Then
>> > MsgBox "Now on page " & Format(page2)
>> > 'Do the necessary operation here...
>> > .
>> > .
>> > .
>> > End If
>> > End Sub
>> >
>> > Unfortunately, the above sub is only works when I click with the mouse
>> > anywhere in the document.
>> >
>> > I tried other subs from the same set and I thought that
>> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
>> > either.
>> >
>> > Can anyone give me a pointer as to how I can trap the 'new character
>> > event'
>> > (if there is one) and to check if the page number has changed ?
>> >
>> > Any help will be greatly appreciated !
>> >
>> > Many thanks,
>> > --
>> > Lars
>>
>>


Re: Catching the New Page event in VBA by Doug

Doug
Wed Jul 16 19:36:47 PDT 2008

While Tony has given you code for using VBA to set up the page headers, that
can all be done in the template so that nothing has to be done when the user
is creating the document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Tony Jollans" <My forename at my surname dot com> wrote in message
news:e91RrC25IHA.4448@TK2MSFTNGP05.phx.gbl...
> You have all the information you need to set up the second page header
> right at the beginning - you don't need to wait and see whether or not it
> is needed.
>
> When you do all the other setup stuff, add some code like this:
>
> With ActiveDocument.Sections(1)
> .PageSetup.DifferentFirstPageHeaderFooter = True
> .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever on
> page 1"
> .Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on page
> 2 onwards"
> End With
>
> Excatly what you set up is up to you, of course :-)
>
> When, or if, the user starts a new page the different header will be used
> automatically. If they don't start a new page, they'll never see it. No
> need for any event processing.
>
> --
> Enjoy,
> Tony
>
> "Lars" <Lars@discussions.microsoft.com> wrote in message
> news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
>> Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
>> Windows
>> API calls, as it gets complicated. In answer to your question, when a
>> user
>> creates a new document based on this template, a dialog box opens
>> automatically and the user types in the address and all other info using
>> textboxes and combo boxes. The reference number and the subject of the
>> letter
>> are also entered here (and stored in global string variables). The OK
>> button
>> then inserts the data (with all required paragraph formatting, font
>> selection
>> etc.) such that the user can immediately start typing the body of the
>> letter.
>> The address is positioned on the page so that it fits in the envelope
>> window,
>> and all the other details (date, senders address, signature etc.) are
>> automatically positioned on the page.
>>
>> If the letter is long, and the user types into a second (or even a third)
>> page then, at the moment the new page is created, the reference code and
>> subject of the letter must be inserted into the new page header, using
>> the
>> previously stored data (I use ranges and frames for this part). Life
>> would be
>> SO much easier if a New Page event existed ! Thanks again, Tony for your
>> help
>> :)
>> --
>> Lars
>>
>>
>> "Tony Jollans" wrote:
>>
>>> The Window Selection_Change event is the closest you will get (without
>>> using
>>> heaps of APIs) - and it has some side effects in the UI, but ...
>>>
>>> What is it you want to do when a new page is started?
>>>
>>> --
>>> Enjoy,
>>> Tony
>>>
>>> "Lars" <Lars@discussions.microsoft.com> wrote in message
>>> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
>>> >I am developing an MS Word (xp) template for a corporate letterhead
>>> >that
>>> >will
>>> > automatically insert the reference number (already stored in a
>>> > variable
>>> > from
>>> > the first page) into the header of the second (and subsequent) pages.
>>> >
>>> > I am aware that there is no "new page" event in Word that can be
>>> > caught.
>>> >
>>> > The solution I am working on involves checking the
>>> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property
>>> > and
>>> > running the code as soon as the page number has incremented.
>>> > Unfortunately, I
>>> > cannot find a way of checking this value each time the user types a
>>> > new
>>> > character.
>>> >
>>> > Here is the code I am using at this time :
>>> >
>>> > Option Explicit
>>> > Private WithEvents wdApp As Word.Application
>>> > Dim page1 As Integer
>>> > Dim page2 As Integer
>>> >
>>> > 'This line is executed during the loading of the document
>>> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>>> >
>>> >
>>> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
>>> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
>>> > If page2 > page1 Then
>>> > MsgBox "Now on page " & Format(page2)
>>> > 'Do the necessary operation here...
>>> > .
>>> > .
>>> > .
>>> > End If
>>> > End Sub
>>> >
>>> > Unfortunately, the above sub is only works when I click with the mouse
>>> > anywhere in the document.
>>> >
>>> > I tried other subs from the same set and I thought that
>>> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't
>>> > work
>>> > either.
>>> >
>>> > Can anyone give me a pointer as to how I can trap the 'new character
>>> > event'
>>> > (if there is one) and to check if the page number has changed ?
>>> >
>>> > Any help will be greatly appreciated !
>>> >
>>> > Many thanks,
>>> > --
>>> > Lars
>>>
>>>
>



Re: Catching the New Page event in VBA by Lars

Lars
Wed Jul 16 21:39:01 PDT 2008

Thanks, Macropod, for your idea. This is a different approach, and I find
this interesting as it avoids a lot of VBA. Most of my daily work is with MS
Access development, where almost every event can be caught. I developed a bad
habit of seeing MS Word from this point of view, and therefore miss the
other, and perhaps more obvious ways, within the philosophy of Word !

Many thanks again for your help, Macropod. You have shown me another way,
and I will try this and see. I will let you know how I get on. I am also
learning more about building Word templates :)

--
Lars


"macropod" wrote:

> Hi Lars,
>
> If the paragraph with your reference number is formatted in a Style that isn't used anywhere else in the document, then you could
> use a STYLEREF field to replicate it in the header.
>
> Suppressing the STYLEREF field display on the 1st page is especially easy to manage if the document template is set up with a
> 'different first page' under File|Page Setup|Layout and you put the STYLEREF field into the template's second page header. No vba
> required.
>
> --
> Cheers
> macropod
> [MVP - Microsoft Word]
>
>
> "Lars" <Lars@discussions.microsoft.com> wrote in message news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
> >I am developing an MS Word (xp) template for a corporate letterhead that will
> > automatically insert the reference number (already stored in a variable from
> > the first page) into the header of the second (and subsequent) pages.
> >
> > I am aware that there is no "new page" event in Word that can be caught.
> >
> > The solution I am working on involves checking the
> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property and
> > running the code as soon as the page number has incremented. Unfortunately, I
> > cannot find a way of checking this value each time the user types a new
> > character.
> >
> > Here is the code I am using at this time :
> >
> > Option Explicit
> > Private WithEvents wdApp As Word.Application
> > Dim page1 As Integer
> > Dim page2 As Integer
> >
> > 'This line is executed during the loading of the document
> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >
> >
> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> > If page2 > page1 Then
> > MsgBox "Now on page " & Format(page2)
> > 'Do the necessary operation here...
> > .
> > .
> > .
> > End If
> > End Sub
> >
> > Unfortunately, the above sub is only works when I click with the mouse
> > anywhere in the document.
> >
> > I tried other subs from the same set and I thought that
> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
> > either.
> >
> > Can anyone give me a pointer as to how I can trap the 'new character event'
> > (if there is one) and to check if the page number has changed ?
> >
> > Any help will be greatly appreciated !
> >
> > Many thanks,
> > --
> > Lars
>
>

Re: Catching the New Page event in VBA by Lars

Lars
Wed Jul 16 21:52:04 PDT 2008

Thanks, Tony for your reply. This is also a good approach and I will try
this. As I mentioned in my reply to Macropod, I work mostly in MS Access
development and have a tendency of viewing Word in the same way, assuming
there are lots of events that can be caught. I have a lot of re-learning to
do when it comes to designing Word templates ! I will let you know soon how I
get on.
--
Lars

"Tony Jollans" wrote:

> You have all the information you need to set up the second page header right
> at the beginning - you don't need to wait and see whether or not it is
> needed.
>
> When you do all the other setup stuff, add some code like this:
>
> With ActiveDocument.Sections(1)
> .PageSetup.DifferentFirstPageHeaderFooter = True
> .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever on
> page 1"
> .Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on page
> 2 onwards"
> End With
>
> Excatly what you set up is up to you, of course :-)
>
> When, or if, the user starts a new page the different header will be used
> automatically. If they don't start a new page, they'll never see it. No need
> for any event processing.
>
> --
> Enjoy,
> Tony
>
> "Lars" <Lars@discussions.microsoft.com> wrote in message
> news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
> > Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
> > Windows
> > API calls, as it gets complicated. In answer to your question, when a user
> > creates a new document based on this template, a dialog box opens
> > automatically and the user types in the address and all other info using
> > textboxes and combo boxes. The reference number and the subject of the
> > letter
> > are also entered here (and stored in global string variables). The OK
> > button
> > then inserts the data (with all required paragraph formatting, font
> > selection
> > etc.) such that the user can immediately start typing the body of the
> > letter.
> > The address is positioned on the page so that it fits in the envelope
> > window,
> > and all the other details (date, senders address, signature etc.) are
> > automatically positioned on the page.
> >
> > If the letter is long, and the user types into a second (or even a third)
> > page then, at the moment the new page is created, the reference code and
> > subject of the letter must be inserted into the new page header, using the
> > previously stored data (I use ranges and frames for this part). Life would
> > be
> > SO much easier if a New Page event existed ! Thanks again, Tony for your
> > help
> > :)
> > --
> > Lars
> >
> >
> > "Tony Jollans" wrote:
> >
> >> The Window Selection_Change event is the closest you will get (without
> >> using
> >> heaps of APIs) - and it has some side effects in the UI, but ...
> >>
> >> What is it you want to do when a new page is started?
> >>
> >> --
> >> Enjoy,
> >> Tony
> >>
> >> "Lars" <Lars@discussions.microsoft.com> wrote in message
> >> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
> >> >I am developing an MS Word (xp) template for a corporate letterhead that
> >> >will
> >> > automatically insert the reference number (already stored in a variable
> >> > from
> >> > the first page) into the header of the second (and subsequent) pages.
> >> >
> >> > I am aware that there is no "new page" event in Word that can be
> >> > caught.
> >> >
> >> > The solution I am working on involves checking the
> >> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property
> >> > and
> >> > running the code as soon as the page number has incremented.
> >> > Unfortunately, I
> >> > cannot find a way of checking this value each time the user types a new
> >> > character.
> >> >
> >> > Here is the code I am using at this time :
> >> >
> >> > Option Explicit
> >> > Private WithEvents wdApp As Word.Application
> >> > Dim page1 As Integer
> >> > Dim page2 As Integer
> >> >
> >> > 'This line is executed during the loading of the document
> >> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >> >
> >> >
> >> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> >> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >> > If page2 > page1 Then
> >> > MsgBox "Now on page " & Format(page2)
> >> > 'Do the necessary operation here...
> >> > .
> >> > .
> >> > .
> >> > End If
> >> > End Sub
> >> >
> >> > Unfortunately, the above sub is only works when I click with the mouse
> >> > anywhere in the document.
> >> >
> >> > I tried other subs from the same set and I thought that
> >> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't work
> >> > either.
> >> >
> >> > Can anyone give me a pointer as to how I can trap the 'new character
> >> > event'
> >> > (if there is one) and to check if the page number has changed ?
> >> >
> >> > Any help will be greatly appreciated !
> >> >
> >> > Many thanks,
> >> > --
> >> > Lars
> >>
> >>
>
>

Re: Catching the New Page event in VBA by Lars

Lars
Wed Jul 16 22:13:01 PDT 2008

Thanks for your reply, Doug. This is a similar approach to the one that
Macropod mentioned previously. My worry was that the content of the second
page header must be changed on the fly to display the reference info that was
typed into the first page. I see that Word is a very different world compared
to MS Access programing, and I must now acquire a lot of new experience in
Word template design. I will study all these solutions and will let you know
the outcome soon. Many thanks again for your help :)
--
Lars


"Doug Robbins - Word MVP" wrote:

> While Tony has given you code for using VBA to set up the page headers, that
> can all be done in the template so that nothing has to be done when the user
> is creating the document.
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "Tony Jollans" <My forename at my surname dot com> wrote in message
> news:e91RrC25IHA.4448@TK2MSFTNGP05.phx.gbl...
> > You have all the information you need to set up the second page header
> > right at the beginning - you don't need to wait and see whether or not it
> > is needed.
> >
> > When you do all the other setup stuff, add some code like this:
> >
> > With ActiveDocument.Sections(1)
> > .PageSetup.DifferentFirstPageHeaderFooter = True
> > .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever on
> > page 1"
> > .Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on page
> > 2 onwards"
> > End With
> >
> > Excatly what you set up is up to you, of course :-)
> >
> > When, or if, the user starts a new page the different header will be used
> > automatically. If they don't start a new page, they'll never see it. No
> > need for any event processing.
> >
> > --
> > Enjoy,
> > Tony
> >
> > "Lars" <Lars@discussions.microsoft.com> wrote in message
> > news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
> >> Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
> >> Windows
> >> API calls, as it gets complicated. In answer to your question, when a
> >> user
> >> creates a new document based on this template, a dialog box opens
> >> automatically and the user types in the address and all other info using
> >> textboxes and combo boxes. The reference number and the subject of the
> >> letter
> >> are also entered here (and stored in global string variables). The OK
> >> button
> >> then inserts the data (with all required paragraph formatting, font
> >> selection
> >> etc.) such that the user can immediately start typing the body of the
> >> letter.
> >> The address is positioned on the page so that it fits in the envelope
> >> window,
> >> and all the other details (date, senders address, signature etc.) are
> >> automatically positioned on the page.
> >>
> >> If the letter is long, and the user types into a second (or even a third)
> >> page then, at the moment the new page is created, the reference code and
> >> subject of the letter must be inserted into the new page header, using
> >> the
> >> previously stored data (I use ranges and frames for this part). Life
> >> would be
> >> SO much easier if a New Page event existed ! Thanks again, Tony for your
> >> help
> >> :)
> >> --
> >> Lars
> >>
> >>
> >> "Tony Jollans" wrote:
> >>
> >>> The Window Selection_Change event is the closest you will get (without
> >>> using
> >>> heaps of APIs) - and it has some side effects in the UI, but ...
> >>>
> >>> What is it you want to do when a new page is started?
> >>>
> >>> --
> >>> Enjoy,
> >>> Tony
> >>>
> >>> "Lars" <Lars@discussions.microsoft.com> wrote in message
> >>> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
> >>> >I am developing an MS Word (xp) template for a corporate letterhead
> >>> >that
> >>> >will
> >>> > automatically insert the reference number (already stored in a
> >>> > variable
> >>> > from
> >>> > the first page) into the header of the second (and subsequent) pages.
> >>> >
> >>> > I am aware that there is no "new page" event in Word that can be
> >>> > caught.
> >>> >
> >>> > The solution I am working on involves checking the
> >>> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")' property
> >>> > and
> >>> > running the code as soon as the page number has incremented.
> >>> > Unfortunately, I
> >>> > cannot find a way of checking this value each time the user types a
> >>> > new
> >>> > character.
> >>> >
> >>> > Here is the code I am using at this time :
> >>> >
> >>> > Option Explicit
> >>> > Private WithEvents wdApp As Word.Application
> >>> > Dim page1 As Integer
> >>> > Dim page2 As Integer
> >>> >
> >>> > 'This line is executed during the loading of the document
> >>> > page1 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >>> >
> >>> >
> >>> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> >>> > page2 = ActiveDocument.BuiltInDocumentProperties("number of pages")
> >>> > If page2 > page1 Then
> >>> > MsgBox "Now on page " & Format(page2)
> >>> > 'Do the necessary operation here...
> >>> > .
> >>> > .
> >>> > .
> >>> > End If
> >>> > End Sub
> >>> >
> >>> > Unfortunately, the above sub is only works when I click with the mouse
> >>> > anywhere in the document.
> >>> >
> >>> > I tried other subs from the same set and I thought that
> >>> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't
> >>> > work
> >>> > either.
> >>> >
> >>> > Can anyone give me a pointer as to how I can trap the 'new character
> >>> > event'
> >>> > (if there is one) and to check if the page number has changed ?
> >>> >
> >>> > Any help will be greatly appreciated !
> >>> >
> >>> > Many thanks,
> >>> > --
> >>> > Lars
> >>>
> >>>
> >
>
>
>

Re: Catching the New Page event in VBA by Doug

Doug
Wed Jul 16 23:06:03 PDT 2008

I think that you said that you had a dialog box opening when the user
created a new document and by that I assume that you mean a userform. Given
that the required information will be entered into the controls on that form
by the user, you can then set the values of document variables so that they
store that information and then you can have it displayed anywhere in your
document, including the primary header (the one that will appear on the
second and subsequent pages if you have the Different first page option
selected under the Layout tab of the Page Setup dialog) by the use of {
DOCVARIABLE } fields.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Lars" <Lars@discussions.microsoft.com> wrote in message
news:8B68AE80-1847-4C7F-9BD8-2A5E00DEB6A8@microsoft.com...
> Thanks for your reply, Doug. This is a similar approach to the one that
> Macropod mentioned previously. My worry was that the content of the second
> page header must be changed on the fly to display the reference info that
> was
> typed into the first page. I see that Word is a very different world
> compared
> to MS Access programing, and I must now acquire a lot of new experience in
> Word template design. I will study all these solutions and will let you
> know
> the outcome soon. Many thanks again for your help :)
> --
> Lars
>
>
> "Doug Robbins - Word MVP" wrote:
>
>> While Tony has given you code for using VBA to set up the page headers,
>> that
>> can all be done in the template so that nothing has to be done when the
>> user
>> is creating the document.
>>
>> --
>> Hope this helps.
>>
>> Please reply to the newsgroup unless you wish to avail yourself of my
>> services on a paid consulting basis.
>>
>> Doug Robbins - Word MVP
>>
>> "Tony Jollans" <My forename at my surname dot com> wrote in message
>> news:e91RrC25IHA.4448@TK2MSFTNGP05.phx.gbl...
>> > You have all the information you need to set up the second page header
>> > right at the beginning - you don't need to wait and see whether or not
>> > it
>> > is needed.
>> >
>> > When you do all the other setup stuff, add some code like this:
>> >
>> > With ActiveDocument.Sections(1)
>> > .PageSetup.DifferentFirstPageHeaderFooter = True
>> > .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever
>> > on
>> > page 1"
>> > .Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on
>> > page
>> > 2 onwards"
>> > End With
>> >
>> > Excatly what you set up is up to you, of course :-)
>> >
>> > When, or if, the user starts a new page the different header will be
>> > used
>> > automatically. If they don't start a new page, they'll never see it. No
>> > need for any event processing.
>> >
>> > --
>> > Enjoy,
>> > Tony
>> >
>> > "Lars" <Lars@discussions.microsoft.com> wrote in message
>> > news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
>> >> Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
>> >> Windows
>> >> API calls, as it gets complicated. In answer to your question, when a
>> >> user
>> >> creates a new document based on this template, a dialog box opens
>> >> automatically and the user types in the address and all other info
>> >> using
>> >> textboxes and combo boxes. The reference number and the subject of the
>> >> letter
>> >> are also entered here (and stored in global string variables). The OK
>> >> button
>> >> then inserts the data (with all required paragraph formatting, font
>> >> selection
>> >> etc.) such that the user can immediately start typing the body of the
>> >> letter.
>> >> The address is positioned on the page so that it fits in the envelope
>> >> window,
>> >> and all the other details (date, senders address, signature etc.) are
>> >> automatically positioned on the page.
>> >>
>> >> If the letter is long, and the user types into a second (or even a
>> >> third)
>> >> page then, at the moment the new page is created, the reference code
>> >> and
>> >> subject of the letter must be inserted into the new page header, using
>> >> the
>> >> previously stored data (I use ranges and frames for this part). Life
>> >> would be
>> >> SO much easier if a New Page event existed ! Thanks again, Tony for
>> >> your
>> >> help
>> >> :)
>> >> --
>> >> Lars
>> >>
>> >>
>> >> "Tony Jollans" wrote:
>> >>
>> >>> The Window Selection_Change event is the closest you will get
>> >>> (without
>> >>> using
>> >>> heaps of APIs) - and it has some side effects in the UI, but ...
>> >>>
>> >>> What is it you want to do when a new page is started?
>> >>>
>> >>> --
>> >>> Enjoy,
>> >>> Tony
>> >>>
>> >>> "Lars" <Lars@discussions.microsoft.com> wrote in message
>> >>> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
>> >>> >I am developing an MS Word (xp) template for a corporate letterhead
>> >>> >that
>> >>> >will
>> >>> > automatically insert the reference number (already stored in a
>> >>> > variable
>> >>> > from
>> >>> > the first page) into the header of the second (and subsequent)
>> >>> > pages.
>> >>> >
>> >>> > I am aware that there is no "new page" event in Word that can be
>> >>> > caught.
>> >>> >
>> >>> > The solution I am working on involves checking the
>> >>> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")'
>> >>> > property
>> >>> > and
>> >>> > running the code as soon as the page number has incremented.
>> >>> > Unfortunately, I
>> >>> > cannot find a way of checking this value each time the user types a
>> >>> > new
>> >>> > character.
>> >>> >
>> >>> > Here is the code I am using at this time :
>> >>> >
>> >>> > Option Explicit
>> >>> > Private WithEvents wdApp As Word.Application
>> >>> > Dim page1 As Integer
>> >>> > Dim page2 As Integer
>> >>> >
>> >>> > 'This line is executed during the loading of the document
>> >>> > page1 = ActiveDocument.BuiltInDocumentProperties("number of
>> >>> > pages")
>> >>> >
>> >>> >
>> >>> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
>> >>> > page2 = ActiveDocument.BuiltInDocumentProperties("number of
>> >>> > pages")
>> >>> > If page2 > page1 Then
>> >>> > MsgBox "Now on page " & Format(page2)
>> >>> > 'Do the necessary operation here...
>> >>> > .
>> >>> > .
>> >>> > .
>> >>> > End If
>> >>> > End Sub
>> >>> >
>> >>> > Unfortunately, the above sub is only works when I click with the
>> >>> > mouse
>> >>> > anywhere in the document.
>> >>> >
>> >>> > I tried other subs from the same set and I thought that
>> >>> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't
>> >>> > work
>> >>> > either.
>> >>> >
>> >>> > Can anyone give me a pointer as to how I can trap the 'new
>> >>> > character
>> >>> > event'
>> >>> > (if there is one) and to check if the page number has changed ?
>> >>> >
>> >>> > Any help will be greatly appreciated !
>> >>> >
>> >>> > Many thanks,
>> >>> > --
>> >>> > Lars
>> >>>
>> >>>
>> >
>>
>>
>>



Re: Catching the New Page event in VBA by Lars

Lars
Sun Jul 20 05:26:02 PDT 2008

IT WORKS ! Thanks, Doug, for your help. You assumed correctly, I did mean a
userform rather than dialogue box (oops !). Your solution to insert a field
directly into the second page header is a good approach (the header also
contains a logo at the top, and a date and page number field on the right
side) This is a much simpler solution than to insert ranges and frames. So
into the 2nd page header of the template, I inserted the field : {DOCVARIABLE
"LetterRef1"\*MERGEFORMAT}

I then added the following code to the OK button:
with ActiveDocument
.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
.Variables.Add Name:="LetterRef1", Value:=Me.txtOurRef.Value
'To display the result, update all fields in header
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Update
End With

It is not necessary to use a 'For Each section' loop, as there is only one
section and one (new page) header. When I created a new document using this
template, then typed my way onto a new page, the letter reference appeared in
the new page header as planned :)

Wow ! All this is new to me and I now have a lot of learning to do when
creating Word templates and to understand more about the Word object model
and its collections. I will now close this thread with big thanks to all of
you for getting me on the right track and helping me to get off to a good
start in this domain.

It now seems that a New Page event in Word is not necessary after all...

--
Lars


"Doug Robbins - Word MVP" wrote:

> I think that you said that you had a dialog box opening when the user
> created a new document and by that I assume that you mean a userform. Given
> that the required information will be entered into the controls on that form
> by the user, you can then set the values of document variables so that they
> store that information and then you can have it displayed anywhere in your
> document, including the primary header (the one that will appear on the
> second and subsequent pages if you have the Different first page option
> selected under the Layout tab of the Page Setup dialog) by the use of {
> DOCVARIABLE } fields.
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "Lars" <Lars@discussions.microsoft.com> wrote in message
> news:8B68AE80-1847-4C7F-9BD8-2A5E00DEB6A8@microsoft.com...
> > Thanks for your reply, Doug. This is a similar approach to the one that
> > Macropod mentioned previously. My worry was that the content of the second
> > page header must be changed on the fly to display the reference info that
> > was
> > typed into the first page. I see that Word is a very different world
> > compared
> > to MS Access programing, and I must now acquire a lot of new experience in
> > Word template design. I will study all these solutions and will let you
> > know
> > the outcome soon. Many thanks again for your help :)
> > --
> > Lars
> >
> >
> > "Doug Robbins - Word MVP" wrote:
> >
> >> While Tony has given you code for using VBA to set up the page headers,
> >> that
> >> can all be done in the template so that nothing has to be done when the
> >> user
> >> is creating the document.
> >>
> >> --
> >> Hope this helps.
> >>
> >> Please reply to the newsgroup unless you wish to avail yourself of my
> >> services on a paid consulting basis.
> >>
> >> Doug Robbins - Word MVP
> >>
> >> "Tony Jollans" <My forename at my surname dot com> wrote in message
> >> news:e91RrC25IHA.4448@TK2MSFTNGP05.phx.gbl...
> >> > You have all the information you need to set up the second page header
> >> > right at the beginning - you don't need to wait and see whether or not
> >> > it
> >> > is needed.
> >> >
> >> > When you do all the other setup stuff, add some code like this:
> >> >
> >> > With ActiveDocument.Sections(1)
> >> > .PageSetup.DifferentFirstPageHeaderFooter = True
> >> > .Headers(wdHeaderFooterFirstPage).Range.InsertBefore "whatever
> >> > on
> >> > page 1"
> >> > .Headers(wdHeaderFooterPrimary).RangeInsertBefore "whatever on
> >> > page
> >> > 2 onwards"
> >> > End With
> >> >
> >> > Excatly what you set up is up to you, of course :-)
> >> >
> >> > When, or if, the user starts a new page the different header will be
> >> > used
> >> > automatically. If they don't start a new page, they'll never see it. No
> >> > need for any event processing.
> >> >
> >> > --
> >> > Enjoy,
> >> > Tony
> >> >
> >> > "Lars" <Lars@discussions.microsoft.com> wrote in message
> >> > news:A92B9011-4F5B-4702-9ED4-CF60A27922FB@microsoft.com...
> >> >> Thanks, Tony, for your prompt reply. As you say, it is wise to avoid
> >> >> Windows
> >> >> API calls, as it gets complicated. In answer to your question, when a
> >> >> user
> >> >> creates a new document based on this template, a dialog box opens
> >> >> automatically and the user types in the address and all other info
> >> >> using
> >> >> textboxes and combo boxes. The reference number and the subject of the
> >> >> letter
> >> >> are also entered here (and stored in global string variables). The OK
> >> >> button
> >> >> then inserts the data (with all required paragraph formatting, font
> >> >> selection
> >> >> etc.) such that the user can immediately start typing the body of the
> >> >> letter.
> >> >> The address is positioned on the page so that it fits in the envelope
> >> >> window,
> >> >> and all the other details (date, senders address, signature etc.) are
> >> >> automatically positioned on the page.
> >> >>
> >> >> If the letter is long, and the user types into a second (or even a
> >> >> third)
> >> >> page then, at the moment the new page is created, the reference code
> >> >> and
> >> >> subject of the letter must be inserted into the new page header, using
> >> >> the
> >> >> previously stored data (I use ranges and frames for this part). Life
> >> >> would be
> >> >> SO much easier if a New Page event existed ! Thanks again, Tony for
> >> >> your
> >> >> help
> >> >> :)
> >> >> --
> >> >> Lars
> >> >>
> >> >>
> >> >> "Tony Jollans" wrote:
> >> >>
> >> >>> The Window Selection_Change event is the closest you will get
> >> >>> (without
> >> >>> using
> >> >>> heaps of APIs) - and it has some side effects in the UI, but ...
> >> >>>
> >> >>> What is it you want to do when a new page is started?
> >> >>>
> >> >>> --
> >> >>> Enjoy,
> >> >>> Tony
> >> >>>
> >> >>> "Lars" <Lars@discussions.microsoft.com> wrote in message
> >> >>> news:34EA4365-1F35-4EAD-8376-7F3914361D21@microsoft.com...
> >> >>> >I am developing an MS Word (xp) template for a corporate letterhead
> >> >>> >that
> >> >>> >will
> >> >>> > automatically insert the reference number (already stored in a
> >> >>> > variable
> >> >>> > from
> >> >>> > the first page) into the header of the second (and subsequent)
> >> >>> > pages.
> >> >>> >
> >> >>> > I am aware that there is no "new page" event in Word that can be
> >> >>> > caught.
> >> >>> >
> >> >>> > The solution I am working on involves checking the
> >> >>> > 'ActiveDocument.BuiltInDocumentProperties("number of pages")'
> >> >>> > property
> >> >>> > and
> >> >>> > running the code as soon as the page number has incremented.
> >> >>> > Unfortunately, I
> >> >>> > cannot find a way of checking this value each time the user types a
> >> >>> > new
> >> >>> > character.
> >> >>> >
> >> >>> > Here is the code I am using at this time :
> >> >>> >
> >> >>> > Option Explicit
> >> >>> > Private WithEvents wdApp As Word.Application
> >> >>> > Dim page1 As Integer
> >> >>> > Dim page2 As Integer
> >> >>> >
> >> >>> > 'This line is executed during the loading of the document
> >> >>> > page1 = ActiveDocument.BuiltInDocumentProperties("number of
> >> >>> > pages")
> >> >>> >
> >> >>> >
> >> >>> > Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
> >> >>> > page2 = ActiveDocument.BuiltInDocumentProperties("number of
> >> >>> > pages")
> >> >>> > If page2 > page1 Then
> >> >>> > MsgBox "Now on page " & Format(page2)
> >> >>> > 'Do the necessary operation here...
> >> >>> > .
> >> >>> > .
> >> >>> > .
> >> >>> > End If
> >> >>> > End Sub
> >> >>> >
> >> >>> > Unfortunately, the above sub is only works when I click with the
> >> >>> > mouse
> >> >>> > anywhere in the document.
> >> >>> >
> >> >>> > I tried other subs from the same set and I thought that
> >> >>> > 'wdApp_DocumentChange()' would be the one to use, but this dosen't
> >> >>> > work
> >> >>> > either.
> >> >>> >
> >> >>> > Can anyone give me a pointer as to how I can trap the 'new
> >> >>> > character
> >> >>> > event'
> >> >>> > (if there is one) and to check if the page number has changed ?
> >> >>> >
> >> >>> > Any help will be greatly appreciated !
> >> >>> >
> >> >>> > Many thanks,
> >> >>> > --
> >> >>> > Lars
> >> >>>
> >> >>>
> >> >
> >>
> >>
> >>
>
>
>