I need to create a field in Word 2007 that will display some text when the its file is not in the expected directory (a folder called "Release"). The drive mapping and top level folders may be different on each machine and for each document.

I tried using

{ IF "{ FILENAME \p }" = "*Release*" "OK" "DOCUMENT OUT OF DATE" }

But this does not give correct results. I also tried using COMPARE but no success. Turns out I can't use two wildcards at a time, see: Word mergefield wildcard not correctly matching

Does anyone know a workaround to this or can anyone suggest a workaround?

I can't easily implement a VBA solution as none of the users will activate the document's macro's or trust certificates and management are reluctant to allow trusted locations.


For future readers, the original suggestion (at the bottom) may still be useful as long as you know the paths are short.

I have modified the recent suggesiton because in its original form, when the document was not in the Release folder but a document with the same name and fields existed in the Release folder, that document would try to include itself until Word reached its field nesting limit (around 19/20 levels) and then pop up an error dialog. The other problem that may exist with this approach is that Word may pop up a dialog in some cases anyway (I found that it did with Word 2010/Win 7 when the document was in a UNC path but not a driveletter: type path, but I do not know exactly what circumstances and settings will cause that problem. AFAICS the field still works correctly in that situation, but the dialog pop-up would almost certainly be unsettling (at the very least) for an end user).

To make this work, you have to insert a bookmark that does not include the { IF } field. To make it harder for the user to delete, you can for example click just before the "Error!" text and insert a point bookmark there. I have used the bookmark name "bkmk_release"

Otherwise, the best I can manage is this, which relies on a Windows pathname "trick" that generally works in Word, and works beyond the 128-character path limit (I suspect the next limit will be a 256-char total.):

{ IF "{ INCLUDETEXT "{ FILENAME \p }\\..\\..\\Release\\{ FILENAME }" bkmk_rlease }" = "Error! Include may not refer to itself." "DOCUMENT OUT OF DATE" "OK" }

i.e. it probably won't work if your file could legitimately be at an http:// address. As it stands, it relies on an English-language setup. You may be able to improve that using

{ IF "{ INCLUDETEXT "{ FILENAME \p }\\..\\..\\Release\\{ FILENAME }" bkmk_release }" = "{ INCLUDETEXT "{ FILENAME \p }" }" "DOCUMENT OUT OF DATE" "OK" }

but I have not checked.

In other words, if the file is in the Release folder, it is trying to include itself and the INCLUDETEXT should return the error. If the file is not in the Release folder, the includetext will try to open the same-named file in the Release folder. If that does not exist, you will see a different error (probably more than one possibility there), e.g. doesn't exist, invalid name. The only problem is if it does exist and contains the text "Error! Include may not refer to itself." :-)

There may be some other "edge cases", e.g. if the document is in the top-level folder.

The original suggestion:

As long as Release is always the folder containing the file, not a folder further up the hierarchy, something like this should do it:

{ IF "{ INCLUDETEXT "{ FILENAME \p }\\..\\..\\Release\\{ FILENAME }" }" = "{ INCLUDETEXT "{ FILENAME \p }" }" "DOCUMENT OUT OF DATE" "OK" }

Although thinking about this further, there may be a problem with longer pathnames - even 128 characters may be a limitation, which would rule this out in some scenarios.

  • Thanks, I didn't think of using the { FILENAME } field on the right, but unfortunately there are several folders under the Release folder. You are also correct on the 128 character limit. I get "Error! Trailing portion of string too long for wildcard match" when I test with long paths. – Redchair Mar 30 '14 at 23:12
  • @Redchair: I posted an alternative which I haven't subjected to much testing. The field-only ideas I have beyond that are nastier (e.g. rely on the existence of files in fixed external locations) and even less likely to work. – user181946 Mar 31 '14 at 8:30
  • Thanks for your help babdia. That approach is very clever. But I can't get it working. When I use { INCLUDETEXT "{ FILENAME \p }\\..\\..\\Release\\{ FILENAME }" } on its own I get the error 'Error! Bookmark not defined'... As far as I can tell I am not specifying a bookmark. – Redchair Apr 4 '14 at 4:38
  • The filepath returned by FILENAME \p uses single backslashes while it seems includetext requires doubles. FILENAME /p also returns the filename not just the path :-s – Redchair Apr 4 '14 at 4:45
  • Well, it seems I commented too soon. Updating fields and the error I get is 'Error! Not a valid filename'. Which makes sense. The field works out to be something like { INCLUDETEXT "\\Networkserver\drive\department\Staff\Release\SOPs\TEST.doc\\..\\..\\Release\\TEST.doc" } which doesn't seem like correct syntax. – Redchair Apr 4 '14 at 4:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.