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
> >> >>>
> >> >>>
> >> >
> >>
> >>
> >>
>
>
>