ArcPy MXD Linked Word Doc – Refreshing Linked Word Doc Objects in MXD Using ArcPy

arcmaparcpymxdrefresh

I have multiple MXDs which have linked Word documents added using Insert Object. These Word documents change frequently and need to be updated in the maps. The only way I have found so far, from searches and Esri documentation, requires the MXD to be opened and the linked object double-clicked.

I'm looking for a way to do this using ArcPy, so that the process can be done in batch and without manual MXD opening.

Is there a way to refresh linked objects inserted into ArcMap without manually opening each MXD using ArcPy?

This is in ArcGIS Desktop 10.6.

Best Answer

The refreshing of a linked Word document does not automatically happen as you have discovered and stated in the KB document. Amazingly this was reported in 2016 and ESRI have still (and I guess highly unlikely) not fixed it.

But all it not lost! I experimented with some code, running it in the Python console in Arcmap and I managed to get the layout linked object to refresh.

The trick is that the object must be named uniquely to identify it from all other graphic elements in the layout. I used a simple naming convention of "w0", I will explain the number portion a bit later.

First ensure you linked correctly as shown below (note checkbox tick):

Insert object dialog

Now ensure our linked word document object has the name "w0"

Element properties dialog

Now here is the code:

import arcpy
mxd = arcpy.mapping.MapDocument(r"CURRENT")

# Code assumes element is tagged with a consistent name that is different to all other names to identify it as a linked word document
# in this example the graphic element that is a linked word document is initially named w0

for elm in arcpy.mapping.ListLayoutElements(mxd,"GRAPHIC_ELEMENT","w*"):
    name = elm.name # returns something like w1
    print(name)

    # Get numeric portion of name and increment
    n = int(name[1:])
    n += 1
    newname = "w" + str(n)

    # Clone element and destroy old one
    elm.clone(newname) # Creates a copy with the odd name of w1w2
    elm.delete()

    # We need to correct the cloned name
    oddname = name + newname
    for elm2 in arcpy.mapping.ListLayoutElements(mxd,"GRAPHIC_ELEMENT",oddname):
        elm2.name = newname

arcpy.RefreshActiveView()

The code locates the element by name, increments the number portion so w0 becomes w1 , then clones it and deletes the old graphic element. Final step is to reset the name for the cloned element back to the format w1. The act of cloning and resetting its name has the effect of double clicking on the element to get it to refresh.

Note: I ran this from the python console within ArcMap and it worked well, I have not tried to run it outside an ArcMap session, that's for you to explore if need be.

Related Question