Results 1 to 1 of 1
  1. #1
    Super Moderator
    Join Date
    May 2002
    Canberra, Australian Capital Territory, Australia
    Thanked 188 Times in 172 Posts

    Word Fields and Relative Paths to External Files

    Word tends to store paths to external files with absolute paths. The field affect by this include Word's INCLUDEPICTURE, INCLUDETEXT, LINK, RD and HYPERLINK fields. This means that, should you need to move the files as a group to another folder (eg putting them on a CD or USB stick for portability), all the links will break. What is needed is a means of coercing Word into applying some sort of relative path addressing for fields pointing to other files. Even if you omit the path from the field code, in most Word versions it still gets stored in the file's metadata. The only version I know of that doesn't always do this is Word 2010 - and even that seems to vary according to whether the path has ever been stored...

    Well, here's a solution (based on an idea originally developed by Peter Jamieson, another MS Word MVP). The solution uses an INCLUDEPICTURE field as an example:

    First off, insert a FILENAME field, with the path switch (see under Insert|Field [Word 2003 7 earlier] or Insert|Quick Parts|Field [Word 2007 & later]), anywhere in your document.
    Next, press Alt-F9 to reveal your document's field codes. A FILENAME field will look like:
    {FILENAME \p}
    Similarly, your INCLUDEPICTURE field will look something like:
    {INCLUDEPICTURE "C:\\Users\\UserName\\My Documents\\My Pictures\\Image.png "}
    {INCLUDEPICTURE "C:/Users/UserName/My Documents/My Pictures/Image.png "}
    Some fields might have other switches after the file's name, which you might want to keep, but they aren't relevant to the rest of the discussion below. You can find out what the switches are for from Word's help file, or via Insert|Field.

    To make the INCLUDEPICTURE field look in the current folder, cut & paste the FILENAME field into it, replacing the document path and inserting '\\..\\' or '/ /' between that and the hyperlink file's name, thus:
    {INCLUDEPICTURE "{FILENAME \p}/../Image.png"}
    {INCLUDEPICTURE "{FILENAME \p}\\..\\Image.png"}

    How this works: The FILENAME p field extracts the current file's name and path. The following '\\..\\' or '/../' tells Word to ignore the filename and get just the path. Then all you need to do is to add the source file's name & extension.

    The same approach can be extended to implement a form of relative addressing. For example:
    {INCLUDEPICTURE "{FILENAME \p}\\..\\My PicturesImage.png"}
    looks in the child folder named 'My Pictures' and:
    {INCLUDEPICTURE "{FILENAME \p}\\..\\..\\Image.png"}
    looks in the parent folder, while:
    {INCLUDEPICTURE "{FILENAME \p}\\..\\..\\My Pictures\\Image.png"}
    looks in the parent folder, then its child folder named 'My Pictures' (a sibling folder, so to speak).

    Note: You can go down the file tree by adding however many sets of '\\..\\' you need, but you must give explicit folder names for navigating back up it.

    If you've got a lot of links to convert, you can use Find/Replace to do the grunt work. For example suppose you've got a document in the folder 'C:\Users\UserName\Documents' and a series of INCLUDEPICTURE fields coded like '{INCLUDEPICTURE "C:\\Users\\UserName\\Documents\\Images\\MyPic.jpg "}'. In this case, the images are in a child folder hanging off the 'Documents' folder. The process you'd use is:
    1. Press Alt-F9 to reveal the field codes.
    2. Copy 'INCLUDEPICTURE "C:\\Users\\UserName\\Documents' and paste it into the 'Find' box. Do a 'Find' to lock it in.
    3. Create a FILENAME field, with the path switch (see above) and insert 'INCLUDEPICTURE "' into the document immediately before it.
    4. Cut the 'INCLUDEPICTURE "' and FILENAME field from the document. This puts them in the Windows Clipboard.
    5. Insert '^c' in the 'Replace box.
    6. Click on 'Replace All'.
    7. Copy '\\Images' and paste it into the 'Find' box.
    8. Type '\\ ^&' into the 'Replace' box.
    9. Click on 'Replace All'.
    10. Press Ctrl-A then F9 to update the fields.
    11. Press Alt-F9 to hide the field codes.

    Note: The reason for the 'INCLUDEPICTURE "' copying & cutting is to avoid updating other kinds of links. You can omit that part if you're working with multiple kinds of links, except for LINK fields.

    The above solution works with INCLUDEPICTURE, INCLUDETEXT, RD and HYPERLINK fields, but not with LINK fields. It doesn't work with LINK fields because they have the nasty habit of converting any embedded fields to plain text immediately they're updated.

    Also attached is a macro I've created for automatically updating the field paths for INCLUDEPICTURE, INCLUDETEXT, RD, HYPERLINK and LINK fields and for other linked objects. To use it, simply extract the file from the zip archive, open up the Word VBE (Alt-F11) and import the file into the document you want to use it with, then save the document.


    Note: Any discussions should be made in a new thread - they cannot be added to this one.
    Attached Files Attached Files
    Last edited by macropod; 2014-09-23 at 17:27. Reason: New & Improved macro

    Paul Edstein
    [MS MVP - Word]

  2. The Following 4 Users Say Thank You to macropod For This Useful Post:

    chiccomarad (2014-09-23),igavuzzo (2014-01-21),JohnLTR (2014-08-06),kaybee (2014-03-06)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts