locked
How can I create a conditional header using IF and STYLEREF? RRS feed

  • Question

  • I have a case where sometimes there's a "subsection" header and sometimes there isn't. If there is, I want it to show up after the "section" name in the header. But if there isn't one in THAT section, I just want the "section" name. Let's call the "section" style "My H1" and the "subsection" style "My H2. So I want to do something like this:

    {IF {STYLEREF "My H1"} ": {STYLEREF "My H2"}" ""}

    The problem I have is that I end up getting the NEXT Subsection, even if it's in a new section of the document. I wish I could say look only from the last "My H1" to the next "My H1" and don't see anything before or after that. Is there a way to do something like that?

    Tuesday, December 20, 2011 5:46 PM

Answers

  • Hi,

    So I want to do something like this: {IF {STYLEREF "My H1"} ": {STYLEREF "My H2"}" ""}

    I think there is no solution. Maybe, for numbered headings you could find a workaround using a format switch for DATE (\@).
    Imagine outline numbering this way: "1 | 1.1 | 1.2 | 2 | 3 | 3.1 | 3.2"
    The numbers of { STYLEREF "My H1" \n } will be 1, 2, 3
    The numbers of { STYLEREF "My H2" \n \@ d } will be 1 (1.1), 1 (1.2), 3 (3.1), 3 (3.2)
    So you can compare the numbers: { If { STYLEREF "My H1" \n } = { STYLEREF "My H2" \n \@ d } ": {STYLEREF "My H2" \n }" "" }


    Regards from Germany
    Lisa [MS MVP Word]

    Wednesday, December 21, 2011 1:03 PM

All replies

  • Hi,

    So I want to do something like this: {IF {STYLEREF "My H1"} ": {STYLEREF "My H2"}" ""}

    I think there is no solution. Maybe, for numbered headings you could find a workaround using a format switch for DATE (\@).
    Imagine outline numbering this way: "1 | 1.1 | 1.2 | 2 | 3 | 3.1 | 3.2"
    The numbers of { STYLEREF "My H1" \n } will be 1, 2, 3
    The numbers of { STYLEREF "My H2" \n \@ d } will be 1 (1.1), 1 (1.2), 3 (3.1), 3 (3.2)
    So you can compare the numbers: { If { STYLEREF "My H1" \n } = { STYLEREF "My H2" \n \@ d } ": {STYLEREF "My H2" \n }" "" }


    Regards from Germany
    Lisa [MS MVP Word]

    Wednesday, December 21, 2011 1:03 PM
  • Lisa,

    This is a quite smart solution to a long-standing missing feature! It seems like it exploits a bug, or at least inconsistencies in Word. To get the level 1 value from "Heading 2", I had to use date code \@h rather than \@d, perhaps US vs. Germany? Below are the results of an experiment. Usually Word seems to use an element of the actual date rather than the results of the STYLEREF "Heading 2", except for date codes \@h (gives the level 1 value) and \@m (gives the level 2 value; case-sensitive), at least with PC set to format dates US style. Perhaps with higher heading levels could be had with other date codes.

    The line below just above "4 Heading 1 value 4" shows that your technique works!!

    <Start of header>

    { STYLEREF “Heading 2” \n } = 3.7 Correct. { STYLEREF “Heading 2” \n \@d} = 6 Current day number. { STYLEREF “Heading 2” \n \@dd} = 06 Current day number. { STYLEREF “Heading 2” \n \@m} = 7 ? Heading 2 “3.7” level 2 value. { STYLEREF “Heading 2” \n \@M} = 4 Current month number. { STYLEREF “Heading 2” \n \@mm} = 077 ? Heading 2 “3.7” level 2 value, but three digits? { STYLEREF “Heading 2” \n \@h } = 3 Heading 2 “3.7” level 1 value = 3. { STYLEREF “Heading 2” \n \@hh } = 03 Heading 2 “3.7” level 1 value formatted as hours. { STYLEREF “Heading 2” \n \@hh:mm} = 03:07 Heading 2 value “3.7” formatted at hours and minutes. { STYLEREF “Heading 2” \n \@MMM} = Apr Current month name. { STYLEREF “Heading 2” \n \@MMMM} = April Current month name. { STYLEREF “Heading 2” \n \@y} = 18 Last two digits of current year. { STYLEREF “Heading 2” \n \@yyyy} = 2018 Current year.

    <End of header>

    <Start of body>

    3 Heading 1, value 3

    3.7 Heading 2, value 7

    { STYLEREF “Heading 1” \n } = 3

    { STYLEREF “Heading 2” \n \@h } = 3

    { IF { STYLEREF "Heading 1" \n } = { STYLEREF "Heading 2" \n @h } = “H1=H2" "H1 <> H2" } = H1=H2

    4 Heading 1 value 4

    <End of body>

    Regards,
    Rob

    Saturday, April 7, 2018 1:45 AM
  • Hi Rob,

    As my colleague Lene Fredborg found out, the "\@ h" switch works fine for following language settings: English (United States), English (United Kingdom), French (France), Greek (Greece), Danish (Denmark), Italian (Italy), Spanish (Spain), Hungarian (Hungary).
    It does not work for German settings.
    For German settings you must use the "\@d" switch (d = day).

    Thank you for testing in depth :-),
    Lisa
    Sunday, April 8, 2018 11:30 AM