Share via

Field Code Heading reference

Anonymous
2017-08-21T21:10:40+00:00

I need some help in changing the Style Ref field codes for a non-existent heading.

My headings follow the decimals.

Heading 1 = 3.0

Heading 2 = 3.1

Heading 3 = 3.1.1

Heading 4 = 3.1.1.1

I'm writing a template document (for Doc2) with the use of field codes. Doc1 is a test request, and Doc2 is the test results that references Doc1.

An example being as follows:

For Doc1, Section 3.2.2 is the test setup, and Section 3.3.2 is the test procedure.

For Doc2, Section 3.2.2 gives the results of the test in section 3 of Doc1.

Similarly Doc2 Section 3.2.4 gives the results for Doc1's test setup in Section 3.2.4 and test procedure in Section 3.3.4.

Likewise, Doc2 Section 5.2.15 references Doc1's Sections 5.2.15 (setup) and 5.3.15 (procedure).

Since there are numerous sections for each set of tests, I wanted to use a field code to automatically give the correct test number. This is easy for the test setup references (they have the same heading), but how do I augment my field code to increase the middle heading number ("2" up to "3")?

Using the 3.2.2, Doc2 example, I have the following field code:

Section {STYLEREF  "Heading 3" \n \t}

  Which yields "Section 3.2.2"

How can I get a "Section 3.3.2"? When there is never a"3" used for Heading 2?

Any help would be appreciated! Thank you!

***Post moved by the moderator to the appropriate forum category.***

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-08-23T08:21:39+00:00

re. an equivalent for your SUBSTITUTE expression in Word, there isn't one in the "Field Language", primarily because the Field language does not have any functions like MID, LEFT, INSTR etc. To get string manipulation facilities you basically have to use a DATABASE field connected to a data source that supports an SQL dialect with those kinds of functions. Having a DATABASE field is overkill IMO, and also requires that you have an external file in a known place, which tends to make distribution of documents difficult. It's also "Windows desktop ord only" as Mac Word cannot connect to any such data source. But if you want to pursue that, you could look at the files I posted at http://goo.gl/EYxYij in response to a rather different problem and adapt it for your needs. (Sorry, the problem in there is different enough that it might not be obvious how to adapt the code, but in essence you need to start with a field that looks like

{ QUOTE { DATABASE \d "the database pathname" \s "SELECT '{ STYLEREF 3 \n }'" } }

and use string and logical functions to split the heading number apart and reconstruct it. The total length of the SQL string cannot exceed 255 or perhaps 511 characters but I would think that would be enough in this case. 

If your heading numbering is such that your x.y.z numbers never have y > 9 or z > 9 and "." is the decimal point in your locale and the associated Heading 2 paragraph is always present, I think you can probably get "x.y+1.z" using something like the following:

{ QUOTE { =INT{ STYLEREF 2 \s #0.0 }) }{ =.1+{ STYLEREF 2 \s #.0 } #.0 }{ ={ STYLEREF 3 \s #0.0 }-{ STYLEREF 2 \s #0.0 } #.0 } }

(Not seriously tested!)

That relies on a bit of weirdness in Word's { = } field where it will treat x.y.z as if it is x.y+.z. IN my view it's not such a great idea to rely on what is basically a Word kludge. If y or z can go beyond 9, there is a problem because .1 is arithmetically the same as .10 and so on, so you would then start having to use tests such as { IF { STYLEREF 2 \n } = "*0" } to determine whether "y" is .1 or .10, .2 or 20 and so on, and the same to determine whether z is .1 or .10. I would guess it can be done but I haven't pursued that myself.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2017-08-22T01:20:08+00:00
  1. One possibility would be to use VBA to create Document variables that let you map one number to another. As a simple example, if you know that you need a reference to 3.3.2 but you can only get "3.2.2" using field codes then you can use

Activedocument.Variables("3.2.2").Value = "3.3.2"

Then in your document you would use something like 

{ DOCVARIABLE { STYLEREF 3 \n } }

to display 3.3.2.

If you need more than one mapping, i.e. need to get 3.3.2 and 3.4.2, you could consider

Activedocument.Variables("a3.2.2").Value = "3.3.2"

Activedocument.Variables("b3.2.2").Value = "3.4.2"

then

{ DOCVARIABLE "a{ STYLEREF 3 \n }" }

{ DOCVARIABLE "b{ STYLEREF 3 \n }" }

If there is a regular pattern to the mappings, you may be able to code a simple VBA loop to generate all the necessary mappings, and generate as many as the document needs. You don't have to retain any VBA in the document to make this work, i.e. you can run whatever VBA you need, then just make use of the generated variables using DOCVARIABLE fields.

All the {  } need to be the special field code braces that you can insert using ctrl-F9 on Windows Word.

  1. Personally, I think I would try to find a different way to relate one numbering scheme to the other, but the feasibility and ease of doing that would depend rather a lot on what numbering standards you have to follow.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2017-08-21T23:29:44+00:00

It is not going to be possible to achieve that using a STYLEREF, or any other type of REF field.  You could select the result of the STYLEREF field and then use CTRL+SHIFT+F9 to unlink it from its source and then edit the result to give you what you want.

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-08-25T16:49:18+00:00

    At least a couple of things in there needed fixing...

    { QUOTE { =INT{ STYLEREF 2 \s #0.0 }) }{ =.1+{ STYLEREF 2 \s #.0 } #.0 }{ ={ STYLEREF 3 \s #0.0 }-{ STYLEREF 2 \s #0.0 } #.0 } }

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-08-24T07:03:53+00:00

    Re the styleref field, the best format is probably { STYLEREF 2 \s },

    It isn't well documented - in fact neither the short "2" format nor the \s is mentioned in the ISO standards dcoument for the .docx format - but  this is the STYLEREF coding that Word itself uses when it inserts captions that use a chapter-caption number format. An advantage is that the "2" is locale-independent, whereas the names of "Heading n" styles depend on the display language.

    Looking at it again, I should really have used \s because it is the only switch that will strip out text preceding the heading number such as "Chapter ".

    For STYLEREF fields that return a recognisable number (e.g. a whole number, or a decimal fraction such as 2.12), my initial expectaton would be that you could manipulate the result arithmetically directly in an { = } field, e.g. { +{ STYLEREF }+0.1 }. Hoewever, it turns out that that does not work because Word inserts a left to right mark at the beginning of the result. Some field code types and operations appear to strip that or ignore it (which is probably why the nested DOCVARIABLE works) but others (such as { =}  and { REF } do not. Applying a numeric format does strip/ignore it, so that's the way I went.

    But I got the impression that it was the complete set of field codes that didn't work for you (i.e.  { QUOTE { STYLEREF 2 \n #0 }{ =.1+{ STYLEREF 2 \n #.0 }{ ={ STYLEREF 3 \n #0.0 }-{ STYLEREF 2 \n #0.0 } #.0 } } ). I'll have a nother look, but it will be interesting to see what you come up with.

    Was this answer helpful?

    0 comments No comments