code is placing letter text in 2nd pg header

R

R. Choate

Here is my code which sets up the header for the 2nd page if my document is longer than 1 page (this is from a template app):

MyDate = DateSerial(Year(Date), Month(Date), Day(Date))
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)

.Range.Fields.Add Range:=ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range, _
Type:=wdFieldEmpty, Text:="REF clientname", PreserveFormatting:=False
.Range.InsertAfter vbCr
.Range.InsertAfter Format(MyDate, "mmmm d, yyyy")
.Range.InsertAfter vbCr
.Range.InsertAfter "Page " & _
ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
.Range.Font.Size = 10
.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft

End With

End Sub

Now, I'm getting the entire first page text of the actual letter repeated in the 2nd page, in the header. The stuff I want in the
2nd page header shows up below it. This code is in a sub by itself: and has worked fine for years. I've recently changed the code
which starts the letter so that it calls a template with letterhead on the 1st page only. Hopefully somebody can troubleshoot this
for me. I've spent a lot of time on this but I'm stumped. I know that the problem occurs in this sub.

Thanks !
 
J

Jezebel

There's nothing that leaps out of that code that would copy the body of the
document into the header, unless the 'clientname' bookmark is elsewhere
getting defined as the entire first page.

But why do this at all? Open the template, set up the headers for 'Different
first page'. Insert a page break and format the second page header as you
want it (everything you are currently inserting can be done with fields).
Then delete the page break. Word 'remembers' the second page header and uses
it if your document goes beyond one page.


Separately, this line is pointless:
MyDate = DateSerial(Year(Date), Month(Date), Day(Date))

You're just parsing the date into year, month, day then converting back.
It's exactly the same as

MyDate = Date
 
R

R. Choate

Keep in mind that the letter template does not have any text in it yet at all and it is not a mail merge letter. Each letter created
by the template is different so I don't understand how I can do this with fields. Also, I have to make sure I don't get into a
situation that would force me to abandon my current template in favor of something different, or to move everything over to the
Normal.dot. This is a special interest template that has a ton of code in it and 4 userforms. One of the userforms has a large
amount of code behind it as well. This is for a public accounting firm and generates letters of many types as well as some
non-letter documents. Also, one of the letters created by the template is built to be a shell of a letter which has the user type
the body of the letter after the template program types the date, recipient name and address, and a little spot that shows what
method of delivery is being used for delivery.

Having said those things, I'm open to your ideas. I'm an Excel expert, not a Word expert, despite the fact that I created this app.
That explains why you see things like the "MyDate" line of code. It is possible that I needed a serial number for the date elsewhere
in the code, but for now I'll assume that it was a mistake.

The code I posted is definitely the sub which causes the duplicate text showing up in the 2nd page header. However, I may have
mentioned in my post that this worked perfectly for five years. The change came when I had to add code to apply a 2nd template which
contains the letterhead. Letterhead used to be applied by the print driver for PDF before we started using the actual Adobe Standard
full program. Now I have to apply the letterhead in Word before I call on the print driver. As an aside, the letterhead is only used
under some circumstances, not for every document created.

I'll check to see if the bookmark is somehow getting defined to include the whole first page text. Until then, as mentioned, please
feel free to add comments based on my reply with regard to using fields.

Thanks !

--
RMC,CPA


There's nothing that leaps out of that code that would copy the body of the
document into the header, unless the 'clientname' bookmark is elsewhere
getting defined as the entire first page.

But why do this at all? Open the template, set up the headers for 'Different
first page'. Insert a page break and format the second page header as you
want it (everything you are currently inserting can be done with fields).
Then delete the page break. Word 'remembers' the second page header and uses
it if your document goes beyond one page.


Separately, this line is pointless:
MyDate = DateSerial(Year(Date), Month(Date), Day(Date))

You're just parsing the date into year, month, day then converting back.
It's exactly the same as

MyDate = Date
 
J

Jezebel

R. Choate said:
Keep in mind that the letter template does not have any text in it yet at
all and it is not a mail merge letter. Each letter created
by the template is different so I don't understand how I can do this with
fields. Also, I have to make sure I don't get into a
situation that would force me to abandon my current template in favor of
something different, or to move everything over to the
Normal.dot. This is a special interest template that has a ton of code in
it and 4 userforms. One of the userforms has a large
amount of code behind it as well. This is for a public accounting firm and
generates letters of many types as well as some
non-letter documents. Also, one of the letters created by the template is
built to be a shell of a letter which has the user type
the body of the letter after the template program types the date,
recipient name and address, and a little spot that shows what
method of delivery is being used for delivery.

Not suggesting you abandon your current template -- just modify the headers
in it. Currently your code is inserting your REF field, the date, and what I
guess is meant to be the page number, although it's actually inserting the
number of pages. (No problem if you never go beyond two pages!) The first is
a field anyway; date and page can be inserted as CREATEDATE and PAGE fields
respectively.
Having said those things, I'm open to your ideas. I'm an Excel expert, not
a Word expert, despite the fact that I created this app.
That explains why you see things like the "MyDate" line of code. It is
possible that I needed a serial number for the date elsewhere
in the code, but for now I'll assume that it was a mistake.

'Date' is a serial number to start with, which is why your line seems odd.
This is the case in Excel VBA (and all other VB/VBA platforms) also.
The code I posted is definitely the sub which causes the duplicate text
showing up in the 2nd page header. However, I may have
mentioned in my post that this worked perfectly for five years. The change
came when I had to add code to apply a 2nd template which
contains the letterhead. Letterhead used to be applied by the print driver
for PDF before we started using the actual Adobe Standard
full program. Now I have to apply the letterhead in Word before I call on
the print driver. As an aside, the letterhead is only used
under some circumstances, not for every document created.

I'll check to see if the bookmark is somehow getting defined to include
the whole first page text. Until then, as mentioned, please
feel free to add comments based on my reply with regard to using fields.

To check the bookmark: open a document that's gone wrong. Press F5, select
the bookmark, click Goto -- this will select the bookmark range. There
really is nothing else that could be inserting spurious text into the
header.
 

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