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-23T16:26:03+00:00

    Incidentally, the DATABASE field code would need to be something like this:

    { QUOTE { SET sref3 { STYLEREF 3 \r } }{ DATABASE \d "{ P }" \s "SELECT mid('{ sref3 }',2,instr(1,'{ sref3 }','.')-1&cstr(1+clng(mid('{ sref3 }',instr(1,'{ sref3 }','.')+1,instrrev('{ sref3 }','.')-instr(1'{ sref3 }','.')-1)))&right('{ sref3 }',1+len('{ sref3 }')-instrrev('{ sref3 }','.'))" } }

    (where the bookmrk P contains the path name of the data source file).

    We only extract the text from character 2 onwards because the first character in a { STYLEREF 3 \r } is a unicode left-to-right mark (or it might I suppose be some other mark in an RTL document - I haven't checked).

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2017-08-22T13:53:18+00:00

    Thank you for the clever response. I hadn't considered a number mapping system—which is a much easier solution than my brute force SEQ option that I hadn't got working properly. The VBA is simple and solves the problem of my Heading 3 issue. I had toyed with using a newly defined multilevel list, but it replaced the one that I needed for the rest of the Heading 3 numbering. The VBA idea works well for the template, but takes a while to define all 58 heading instances.

    I had a novel idea that didn't work, but perhaps it could use some tweaking to make it successful, either through the use of a VBA code, or Field code formula. Tell me what you think...

    In excel, if 3.2.3 was written in cell A1, then =SUBSTITUTE(A1,MID(A1,3,1),3) would return 3.3.3

    Is it possible to use it—or something like it—in conjunction with the style ref field code? When I attempted it, I got a Syntax error when I applied the formula as =SUBSTITUTE({ STYLEREF 3 \N },MID({ STYLEREF 3 \N },3,1),3).

    At any rate, thanks for all of the suggestions!

    Was this answer helpful?

    0 comments No comments