Autotext watermarks in multi-section documents

C

Chris J

I am having difficulty with automated insertion of watermarks (eg
diagonal DRAFT) from Autotext. The theory seems to be that, by
inserting these in the header, each page of the document will be
watermarked. But this is not working in practice - I either get the
watermark on only some of the pages, or twice on one page
(superimposed) and not elsewhere.

A typical document where the problem arises is laid out as follows:
- Section break between sections 1 and 2 is on page 1 and continuous
(section 1 first page header contains a letterheading), section 1
primary header not visible because section 1 ends on page 1)
- Section 2 primary header is linked to previous (though behaviour
seems unchanged if I unlink it instead); section 2 doesn't have a
separate first page header, but if it did then presumably this too
would not be visible because first page of section 2 also contains
section 1. Section 2 continues to end of document (assume a 4-page
letter).

The essence of the code I am using (in English rather than VBA) says:
- go to section 2
- if there's a different first page header, go to the first page
header of section 2 and insert the autotext (but this is set to be
skipped for a document configured as described above)
- go to the primary header (here still in section 2) and insert the
autotext

If I leave it at that, I get the watermark only on page 1. (If I
don't set it to skip the stage mentioned, I get the watermark imposed
twice on page 1, and still not elsewhere.)

So I added further lines to say go to page 2 (still with section 2
primary header open) and insert the autotext. That gets the autotext
onto page 2 but not pages 3 and 4. If I extend the code to say repeat
for pages 3 and 4 (which should be irrelevant because they are in
section 2 just as page 2 is), the cursor returns (at the Go To line)
to a frame on page 1 (anchored in section 1), and the
autotext/watermark is then superimposed on the page 1 watermark
(actually rather untidily - one impression is not an exact
superimposition). Nothing happens on page 3 or page 4.

Can anyone explain what's going wrong here please? I could post the
extract of code if needed but I thought it would be easier to explain
as above in the first place.

Thanks

Chris J
 
J

Jezebel

Might be better if you posted the code: it sounds like a bug in the way you
are 'going to' the various headers.

This code iterates all the headers actually defined in your document --

Dim pRange as Word.Range
Dim pIndex as long

For pIndex = 1 to 3
Set pRange = ActiveDocument.StoryRanges(Choose(pIndex,
wdFirstPageHeaderStory, wdEvenPagesHeaderStory, wdOddPagesHeaderStory))
Do until pRange is nothing
... add the watermark to pRange
set pRange = pRange.NextStoryRange
Loop
Next
 
C

Chris J

OK, here is the relevant section "verbatim". I fear imposed line
length limit on postings will cause some lines to wrap around but I
trust this will be obvious and won't cause confusion. A few
explanations follow below the code

For R = SecStart To SecEnd
Selection.GoTo wdGoToSection, R 'ensure all sections get
Watermark
With ActiveDocument.Sections(R)
'insert watermark in first page header of section, if different
from primary section header
If .PageSetup.DifferentFirstPageHeaderFooter = True Then
'don't insert if header is same as previous section and
already inserted there
If R = SecStart _
Or .Headers(wdHeaderFooterFirstPage).LinkToPrevious =
False Then
If LetterDoc = False And BillDoc = False Then
'don't insert watermark in first page header of section 2 of
letters or bills
'continuous section break on first page: superimposition
results
ActiveWindow.ActivePane.View.SeekView =
wdSeekFirstPageHeader
Templates(PHBM).AutoTextEntries(ReqdAT) _
.Insert Where:=Selection.Range, RichText:=True
End If
End If
End If

'insert watermark in primary section header, if not same as
previous where already inserted
If R = SecStart _
Or .Headers(wdHeaderFooterPrimary).LinkToPrevious = False
Then
ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryHeader
Templates(PHBM).AutoTextEntries(ReqdAT) _
.Insert Where:=Selection.Range, RichText:=True
If LetterDoc = True Or (BillDoc = True And ReqdAT =
"WMDraft") Then
If Selection.Information(wdNumberOfPagesInDocument) > 1
Then
Selection.GoTo What:=wdGoToPage, Which:=2
Templates(PHBM).AutoTextEntries(ReqdAT) _
.Insert Where:=Selection.Range, RichText:=True
Selection.GoTo What:=wdGoToSection, Which:=R
End If
End If
End If

'return to main document
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End With
Next R

Explanations: SecStart and SecEnd are the sections (varying according
to the custom template from which the document was prepared) from and
to which (inclusive) the watermark is to be inserted. LetterDoc and
BillDoc are, as you'll guess from the comment, boolean variables which
indicate particular custom templates. Other variables are probably
obvious.

Thanks for the interesting code - I'll defer trying anything based on
this pending further postings on this thread, but one comment at this
stage: where there is a continuous section break so that the page
where it appears is (in part) in each of two or more sections, there
would seem to be a risk that inserting the watermark into every header
in the document could lead to superimposition as I have experienced
already - so I'd possibly need to modify it anyway for that reason.
Do you agree? If you can see where my existing code goes wrong I
think I'd prefer to stick with that basic approach, suitably modified,
if it's not overly cumbersome.

Chris J
 
J

Jezebel

There's no problem about superimpostion simply by inserting the watermark in
every header: only one header gets shown on the page, no matter how many
section breaks there might be on the page. The header and footer displayed
on a page are those for the first section on that page.

A lot of your code will be unnecessary if you simply iterate the StoryRanges
as in the snippet I posted: if you have a series sections, each with its
header 'linked to previous', then there is only one header -- as you will
see if you iterate the StoryRanges list. Hence all that checking whether
Link to Previous is set or whether the sections breaks are continuous will
be unnecessary.
 
C

Chris J

Thanks very much for the reassurance, and for the code extract. It
works like a dream - though I did first have to change
wdOddPagesHeaderStory to wdPrimaryHeaderStory as otherwise I got a
compile error! I'm most grateful - as you say, a lot of previous code
can be dispensed with.

Chris J
 

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