Search code examples
ms-wordfieldcodes

How to do search and replace involving fields in Microsoft Word?


I have a Word document with fields of the reference variety, which occur in the form "[field].[field]"--in other words, there's a period between the two fields. I want to globally replace this with a space.

Word offers the ^d special character to search for fields, but for some reason the query "^d.^d" does not find anything. However, ".^d" does. Now comes the problem, however--what do I specify as the replacement text in order to retain the field code? If using regular expressions, I could use a "Find What Expression" such as \1, but with regexp ("wild card") mode the ^d is not permitted.

I guess I could write a macro...


Solution

  • It's usually better to go the macro route when finding fields because, as you say, the find algorithm that Word uses doesn't work the way you might hope with fields.

    But if you know exactly what the fields contain, you can specify a search pattern that will probably work (however not in wildcard mode).

    For example, if you want to look for figure number field pairs such as

    { STYLEREF 1 \s }.{ SEQ Figure \* ARABIC \s 1 }
    

    (which would typically be the same set of fields everywhere in the document)

    If you only really need to look for the following:

    { STYLEREF 1 \s }.<any field>
    

    you could ensure that field codes are displayed and search for

    ^d STYLEREF 1 \s ^21.^d
    

    or

    ^19 STYLEREF 1 \s ^21.^19
    

    If you need to be more precise, you can spell out the second field as well.

    "^d" only works for finding the field beginning, not the field end.

    It's a shame that ^w wants to find at least 1 whitespace character because otherwise it would be more robust to look for

    ^19^wSTYLEREF^w1^w\s^w^21.^19
    

    Perhaps someone else knows how to work around that without using wildcards?