locked
Word document formating with Field Code: Showing multiline chapter title as single-line page header RRS feed

  • Question

  • I hope Word IT professionals out there might have the answer to the following question at their fingertips. I have been trying to automate my Word document with Styles and Field Codes so that I can delegate the task of automatic chapter numbering and page header formatting to the Word application but this is proving ever more challenging. Having spent several hours by going thru the maze of STYLEREF field code, then considering VBA macros, I came to a stage not to give up the quest for a solution to what appears to be a simple problem. Here goes the problem description.

    My chapter title is formatted with “Heading 1” style. I would like the chapter title to appear on every page header, so in simple cases this can be achieved easily by putting the STYLEREF filed code in the page header as { STYLEREF “Heading 1”}. As expected, this prints a string styled with “Heading 1” (which in this case is the chapter title) wherever  {STYLEREF “Heading 1”} field code appears. So far, so good.


    However, my chapter title deviates from a one-liner format by introducing soft-return characters (^l in Word lingo, i.e., ^ followed by lower case L and achieved by Shift+Enter key) into the title so that a chapter title appears on multiple lines while still retaining the same “Heading 1” style (due to soft break, as opposed to a paragraph break – Enter key). This provides a more elegant format, for instance, I can say:


    Chapter {SEQ chapter}
    <soft break here>
    <soft break here>
    My Chapter Name Here in Bigger Fonts

    The above is styled with “Heading 1” which in turn is formatted as required such as being page-centered, starting with page break, and so on. The field code {SEQ chapter} does the house-keeping job of numbering the chapter.


    What is desired now is to show the chapter title in a page header but as a single line, not as a multiline in the body of the page. Since putting  {STYLEREF “Heading 1”} in the page header produces a multiline title, my initial tack (before changing the whole problem!) was to look into how to convert the soft breaks to empty string or space chars.

    One ideal solution would be {STYLEREF “Heading 1\* RemoveSoftBreaks”} but Field Codes do not provide such options (or obvious concoctions producing same result) beyond the basics such as case conversions {STYLEREF “Heading 1\* Upper} or formatting as in {STYLEREF “Heading 1\* MERGEFORMAT }. I could be wrong here as my knowledge of Field Codes is limited to simple usage and might have overlooked advanced features.

    Better still, another ideal solution would be if paragraph formatting had the option for supressing soft-breaks in which case such formatting could be applied to the page header paragraph. Note that paragraph formatting in Word has Formatting Exceptions (for line numbers and hyphenations, with options selected with checkboxes) and this is a natural home for adding an option of supressing soft-breaks.

    Unfortunately, the above ideal solutions, for now, are just a wish list. From the above long discourse, I hope the question is now clear. {STYLEREF “Heading 1”} being a multiline string in the page header, is there a relatively easy way of removing the ^l characters so as to produce a single line chapter title in the page header? Ideally the solution should be self-updating for every page of the document. Is there any easy VBA macro or even the VSTO approach?

    Alternatively, is there an easy way of modifying a paragraph style with a macro or otherwise, in this case to supress ^l chars from the header paragraph? If a macro solution works for both approaches, it seems to me that working on paragraph style is better than the relatively less familiar field codes.








    • Edited by ucent Tuesday, April 19, 2011 7:38 AM
    Monday, April 18, 2011 2:27 PM

All replies

  • If you can force the line breaks in your Heading 1 styled text by inserting it in a table of restricted width, the StyleRef field will show the text all in a single line.  You can of course turn off the display of the cell borders by using Ctrl+Alt+u


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "ucent" wrote in message news:5f6af867-eb4b-4a88-b54c-c1f26f05231e@communitybridge.codeplex.com...

    I hope Word IT professionals out there might have the answer to the following question at their fingertips. I have been trying to automate my Word document with Styles and Field Codes so that I can delegate the task of automatic chapter numbering and page header formatting to the Word application but this is proving ever more challenging. Having spent several hours by going thru the maze of*STYLEREF* field code, then considering VBA macros, I came to a stage not to give up the quest for a solution to what appears to be a simple problem. Here goes the problem description.

    My chapter title is formatted with *“Heading 1”* style. I would like the chapter title to appear on every page header, so in simple cases this can be achieved easily by putting the STYLEREF filed code in the page header as*{ STYLEREF “Heading 1”}*. As expected, this prints a string styled with*“Heading 1”* (which in this case is the chapter title) wherever *{STYLEREF “Heading 1”}* field code appears. So far, so good.


    However, my chapter title deviates from a one-liner format by introducing soft-return characters (^l in Word lingo, i.e., ^ followed by lower case L and achieved by Shift+Enter key) into the title so that a chapter title appears on multiple lines while still retaining the same *“Heading 1”* style (due to soft break, as opposed to a paragraph break – Enter key). This provides a more elegant format, for instance, I can say:


    *Chapter {SEQ chapter}*
    *<soft break here>*
    *<soft break here>*
    *My Chapter Name Here in Bigger Fonts*

    The above is styled with *“Heading 1”* which in turn is formatted as required such as being page-centered, starting with page break, and so on. The field code*{SEQ chapter}* does the house-keeping job of numbering the chapter.


    What is desired now is to show the chapter title in a page header but as a single line, not as a multiline in the body of the page. Since putting *{STYLEREF “Heading 1”}* in the page header produces a multiline title, my initial tack (before changing the whole problem!) was to look into how to convert the soft breaks to empty string or space chars. The ideal solution would be *{STYLEREF “Heading 1\* RemoveSoftBreaks”}* but Field Codes do not provide such options (or obvious concoctions producing same result) beyond the basics such as case conversions*{STYLEREF “Heading 1\* Upper}* or formatting as in *{STYLEREF “Heading 1\* MERGEFORMAT }*. I could be wrong here as my knowledge of Field Codes is limited to simple usage and might have overlooked advanced features.

    From the above long discourse, I hope the question is now clear. *{STYLEREF “Heading 1”}* being a multiline string in the page header, is there a relatively easy way of removing the ^l characters so as to produce a single line chapter title in the page header? Ideally the solution should be self-updating for every page of the document. Is there any easy VBA macro or even the VSTO approach?





    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Monday, April 18, 2011 8:15 PM
  • Sorry, this doesn't answer the question (which is clearly posed in the last paragraph).
    Monday, April 18, 2011 9:11 PM
  • Hi ucent,

    Here's a way you can achieve what you're after - simply apply two different character Styles to the text above & below the line breaks, then use two StyleRef fields, each pointing to one of those Styles. The character Style definitions needn't have any attributes other than the default font applied - they're simply 'placeholders' for the StyleRef fields.

    Another way would be to format the heading with, say, double line spacing and position a trasnparent, borderless shape (formatted as square/tight) over the part of the first line that you want to leave unused, and use no manual line breaks.

    Yet another way you could achieve the same effect is to configure Word to display and print hidden text and have the manual line breaks formatted as hidden text. This isn't a recommended approach, though, as these settings don't travel with the document and will affect other documents as well.


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Marked as answer by Max Meng Monday, April 25, 2011 2:18 AM
    • Unmarked as answer by ucent Tuesday, April 26, 2011 11:47 AM
    Monday, April 18, 2011 9:30 PM