Link to home
Start Free TrialLog in
Avatar of Bill Golden
Bill GoldenFlag for United States of America

asked on

Word Syntax Error in expanding Page {Page} of {PageNumb} Statement in Header

Syntax Error in expanding Page {Page} of {PageNumb}.

Various literature suggests you can limit the PageNumb aspect of the above statement with

Page {Page} of {={PageNumb}-1}

All I get is a Syntax Error. This function is used when your document contains miscellaneous pages at the end that you do not intend to utilize in (in our case) a pdf file.

Sample file indicating Syntax Error is attached. Any thoughts?
Master-GSKE-Investor-Verification-L.docx
Avatar of Andrew Leniart
Andrew Leniart
Flag of Australia image

Hi Bill,

This Microsoft Add page number X of Y to a document  will give you all the info you need.

Basically, the syntax for what you want to do is as follows;

  1. On the first page, in the Header Area, type Page and press your space bar
  2. Now on the Insert Tab, click Quick Parts and then click Field
  3. Under "Field Names", select "Page" on the left-hand side and choose a Format on the right-hand side Ie: 1,2,3, ...
  4. Click OK
  5. Press your Space Bar and type of and then press your space bar again
  6. Back on the Insert Tab, click Quick Parts and then click Field again
  7. Under "Field Names", select "NumPages" on the left-hand side and choose the same Format on the right-hand side Ie: 1,2,3, ...
  8. Done.

Your pages will now be numbered automatically.

Let me know if you need additional help.

Regards, Andrew
Avatar of Eric Fletcher
The field code you need to use to display the total number of pages is NUMPAGES (i.e. not “PageNumb”).

To edit a field code, press Alt-F9 to toggle visibility between results and the field codes. You can then use normal Word editing to make changes. When finished, press Alt-F9 again and press F9 to recalculate the field to see the updated results.

Suggestion

The approach you are using will display “Page 11 of 38” for page 11 if the document is 38 pages plus one extra page. However, if a different document had four extra pages, you would need to change the calculation to { = {Numpages - 4 } } to display the last relevant page number.

As an alternative that would not require knowing how many extra pages are included, you could include a bookmark at the end of the relevant content and use the PAGEREF cross-reference field code to display the page number.User generated image
The calculation field code is included as an example to show how you could report that information as well if required. (I have seen this used for reporting the number of errata pages for procedure manuals.)

Note: In the screen shots above, the gray shading appears because I have turned on the option in File > Options > Advanced > Show document content, “Field shading = Always”. This causes all field codes and their results to be displayed in on-screen views with the background shading so you can easily tell whether content has been typed or is being calculated. The shading will not be printed. If you use field codes, this is a very useful option to have permanently turned on.

Here's a screen shot of the last “relevant” page in my test document to show how I bookmarked a decorative ending mark so it could be used in the Pageref field code. Note that the status bar is reporting “Page 8 of 12” because I had included 4 extra pages after this mark.
User generated image
(Like the gray field code shading,the gray [ and ] non-printing marks around the bookmark are displayed so I can readily see where content is bookmarked. This option can also be permanently turned on via the “Show bookmarks” checkbox in the same Word Options dialog.)
Avatar of Bill Golden

ASKER

Andrew, thanks for the response. Unfortunately, the document does not address the problem.
Word 2016 inserts the field commands as follows

Page { PAGE   \* MERGEFORMAT } of { NUMPAGES   \* MERGEFORMAT}

Which works identically to

Page { PAGE } of { NUMPAGES }
 (which the nomenclature used in some previous versions of Word.)

There is ample literature about inserting -1, -2, -3, etc. after the NUMPAGES command (but I was unable to find anything about positioning -1 immediately after NUMPAGES or after MERGEFORMAT.  

I have attached a sample file which contains a header line formatted with

Page { PAGE   \* MERGEFORMAT } of { NUMPAGES   \* MERGEFORMAT}

So, I am still looking as to where to insert, in this case, the -1 operand.
Eric, thanks for the response. Unfortunately, your suggested solution does not work!

(I apologize for using the term PAGENUMB instead of NUMPAGES in the initial comment. The supplied example file did use the command NUMPAGES.
 
Actually, Word 2016 inserts the field commands as follows

Page { PAGE   \* MERGEFORMAT } of { NUMPAGES   \* MERGEFORMAT}

Which works identically to

Page { PAGE } of { NUMPAGES }
 (which was the nomenclature used in some previous versions of Word.)

There are ample internet examples about inserting -1, -2, -3, etc. after the NUMPAGES command as per your suggestion. However, I was unable to find anything about positioning -1 when the command contained the operand \* MERGEFORMAT.  

I have attached a sample file which contains the following three different header lines at the top of Page 2 formatted as follows:

Page { PAGE   \* MERGEFORMAT } of { NUMPAGES   \* MERGEFORMAT}
Page { PAGE } of { NUMPAGES }

and I even added a line that reads just...

{={NUMPAGES-1} }

The last line, when toggled, returns a Syntax Error

So, I am still looking as to where to insert the -1 operand.
Master-GSKE-Investor-Verification-L.docx
I assumed you wanted it to report a number that is 1 less than the total number of pages in your document. To do that, you need to use the syntax in my earlier comment.

However, in looking at your document, I can see the problem. Your structure {={NUMPAGES-1}} won't work as entered because the Numpages field has the operator and value within it instead of as a part of the equation field code. If you choose to leave the Mergeformat switch in the field code, it would need to be like this:
{ = { NUMPAGES \* MERGEFORMAT } -1 }
(The spaces are typical of what Word adds, although they are not specifically required.)

Parsing it out as Word will do the calculation: { NUMPAGES } gets resolved first — in your document, as 3. The Mergeformat switch is ignored. The equation field code is then calculated as { = 3 - 1 } to give you 2 as the result.

If you use the UI to insert the field codes, the \* Mergeformat switch typically gets included by default (although you can turn the “Preserve formatting during updates” checkbox off to exclude it). But it and any other switches are part of the field code, and must be included within the automatic braces that Word inserts for you if you press Ctrl-F9 to manually type a field code. They are also included within the braces when you insert a field code with selected switches from the UI (Insert > Quick Parts > Field...).

Note that the equation field code can be handy if you need to do ad hoc math in Word: { = 765 / 19 } will display 40.26. However if you edit the field code to add a numeric picture switch, { = 765 / 19 \# "#,##0.0000" }, it will display as 40.2632. Here, the numeric picture switch is part of the equation field code.

To reinforce the importance of how switches can influence field code results in equations, consider this: { = { Page \* roman } + 3 }. Here, the number switch would cause the digits of the current page number to be resolved as "ii" (instead of 2) first, and you'd see an error. The error in this case would be "undefined bookmark" because the parser would see "ii" as text and try to interpret it as a bookmark. So unless you just happened to have a number set within a bookmark named "ii", the formula would fail.

Try entering the formula you need manually to get a better sense of how it works:
  1. Press Ctrl-F9 to have the cursor positioned between the field code braces
  2. Type = and a space
  3. Press Ctrl-F9 again to insert a new field code with the cursor between the braces
  4. Type Numpages
  5. Now press the right arrow three times to move beyond the } after Numpages
  6. Type -1 and a space
  7. Press F9 to calculate the formula


Remember that Alt-F9 will toggle between field code results and the editable field codes.
Well, I tried but all I ending up getting was an "Undefined Bookmark." It is obviously beyond me. Maybe if I was not still dealing with the aftereffects of chemotherapy I could get it.

Anyway, I was told this morning just to float the file on one of the pay sites which seems ridiculous to fix a single problem. But the delay is holding up the mailout of a number of documents and several people working on the project are going to be furloughed out waiting on one stupid issue.  I will probably cost a couple of hundred dollars to find someone to fix the problem but obviously I cannot get it done.

I am going to take some solace in the fact that it is obviously more difficult than it seems since in supplying an exact example of the file multiple time, I have yet to receive a file that someone has actually made the syntax work!

Thanks for trying.  Bill
ASKER CERTIFIED SOLUTION
Avatar of Eric Fletcher
Eric Fletcher
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much. I did get the download before outsourcing the problem and we are processing numerous documents, some with one, two or three pages of data beyond the basic document.

It appears I can just copy the line at will modifying the number of pages. This will, of course, be very helpful. While I have drafts that "look" exactly like what you sent, it is painfully obvious that the line must be created in a precise manner in order to work.

Thanks again for your help,

Bill
I greatly appreciate all the help.

Bill
Yes, field codes do have pretty specific structures.

If this is something you need frequently Bill, consider adding it as an AutoText entry so you don't need to create it each time. Create it once (and test it), then select it and use Insert > Quick Parts > AutoText, and choose the bottom option to "Save selection to AutoText Gallery". In the resulting dialog, give it a meaningful name (no spaces or punctuation); add a meaningful description so you'll know what it is later; make sure it is saving the item in your Normal template (the default); then click OK.User generated image
Now when you need the formula, just start typing the name. As you type, a pop-up will appear when enough unique letters are entered to enable Word to guess what you want, and you can press Enter to complete the AutoText. For the above example, the popup appeared when I pressed the "l". (This is why it is a good idea to use a name that won't be confused with a normal word.) Alternatively, you can type the AutoText name and press F3 to add the content to your document.

An AutoText entry can include multiple lines or paragraphs, so your entire header block could be included in one. The screen shot below shows the result of my AutoText entry named “ltrhdr”. It uses StyleRef field codes to automatically insert whatever has been set with the “addressee name“ style, then a line with the page number info, followed by another StyleRef to insert the date of the letter (which is tagged by my “date” style). The picture on the right shows how it will appear in the upper right header position of all but the first page of any letter.User generated image
(In fact, this structure is included in my personal letter template, so any letter I create from that template will already have everything set up (including today's date already set with my “date” style and with the cursor on an empty line already tagged with the “addressee name” style). All I need to do is type in the content.)

In your sample document, the large block of names on page 1 could be defined in an AutoText entry. That way, it would simplify entering it when needed, but also, could be modified (edited & resaved) if one of the names or titles changed. When saved in the default Normal template, any such AutoText entries will be available to any document.

Although they may be a pain to deal with initially, field codes can be extremely useful in automating repetitive tasks and making use of content already available. Well worth the effort to learn in my humble opinion!