Programatically determine number of sections

B

Biz Enhancer

Hi,

The problem I have is this.

I have a compound merge document that requires a report footer. The merged
document will always vary in length and number of records merged.

My solution was to try and determine the number of section breaks and use a
mergefield something like the following:
{IF {MERGEFIELD recID} = TotalNoOfSections "this text" "Blank"}

It is not practical to enter the total number of sections manually.

The Mergefield RecID always works with sequential numbers so in theory the
total number of section will always equal the last merged record and trigger
the report footer to show.

This is merging from a database but can not be done as a database report.

Any ideas?

Regards
Nick.
 
D

Doug Robbins - Word MVP

It is not clear how you want to cause the footer to show, but
ActiveDocument.Sections.Count will give the number of sections in a
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
 
M

macropod

Hi Biz Enhancer,

I don't think your proposed approach will work, since every Section is the last Section at the time it is created during a
mailmerge. That and the mailmerge process outputs nothing of its own accord to indicate that the last record has been reached.

However, there is a solution - you could use a field in the footer coded as:
{IF{={NUMPAGES}-{PAGE}-{SECTIONPAGES}}< 0 "Text for every page in the last Section"}

Of course, if the Sections always had only one page, you could use the much simpler:
{IF{NUMPAGES}= {PAGE} "Every Page in the last Section"}

Note: The field brace pairs (ie '{ }') for the above examples are created via Ctrl-F9 - you can't simply type them or copy & paste
them from this message.
 
P

Peter Jamieson

Do these actually work? I've tried various variations here and the
general impression I have is that at the point Word resolves the If
statement, the values are never the ones you need. In Word 2007, Word
seems to make the decision about which IF result to use right at the
beginning of the merge - perhaps that's not the same for earlier versions.

As long as you are outputting to a new document and have continuous page
numbering (e.g. with a Directory merge) I think the following will
work in recent versions of Word (2000 seems to get confused about
NUMPAGES sometimes)

{ ={ NUMPAGES }-{ PAGE } \#";;'last page'" }

{ ={ NUMPAGES }-{ PAGE }-{ SECTIONPAGES } \#";'last section'" }

This is primarily because Word does not resolve the nested fields when
you do it this way.

But of course that will only be OK if the text you want to insert is
relatively small etc.

Peter Jamieson

http://tips.pjmsn.me.uk
 
M

macropod

Hi Peter,

Since the field coding in this case is independent of the merge process (and the fields are thus not converted to plain text), they
remain dynamic and continue to evaluate as more Sections are added to the document. As coded, the output will display in whatever
happens to be the last Section only - add/delete a Section and the the display will re-set itself to the new last Section.
 
P

Peter Jamieson

Hi macropod,

Well, it looks as if the OP has found a solution, but...
in this case is independent of the merge process

....sorry, I get the impression I've missed something really obvious, as
I'm not sure what you mean by this. I can see that the IF fields would
work if they survived the merge, but they don't. At least not here. The
individual { PAGE }, { NUMPAGES } and { SECTIONPAGES } do survive if you
insert them.

So I'm wondering what makes the { IF } construct survive. AFAICS where
you use an { = } and a numeric format switch, the nested field does survive.

Best regards,

Peter Jamieson

http://tips.pjmsn.me.uk
 
M

macropod

Hi Peter,

Ah, I see what you mean. I originally tested the field code with an ordinary document, not with a mailmerge document.

The following construct does survive the merge process:
{={NUMPAGES}-{PAGE}-{SECTIONPAGES} \# ";'Text for every page in the last Section'"}
Note the need for the single quotes - otherwise the 'x' in 'Text' is misinterpreted.
 

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