Solved! Go to Solution.
Janet:
I have had to perform this task in older versions of ArcMap as well where python did not have its current capabilities so VBA was used. In each case, whether it be in python or VBA, a corrupt mxd always crashed the script even with error handling such as try...except was used. I have spoken with ESRI Technical representatives and they have confirmed that there is no way around a corrupt mxd in VBA or python scripting except to exclude it all together from your loop.
In my case, I had a script that would take an inventory of all mxds and various connections such as SDE. This was a read-only step where information was saved to an output text file. When the script would crash on a corrupt mxd, it would be noted and added to the code for the other script, that modified paths to data, so that script would just bypass these corrupt files and hopefully never crash.
I hope this information helps you to come up with a solution.
map_doc = arcpy.mapping.MapDocument('path_to_mxd') data_frames = arcpy.mapping.ListDataFrames(map_doc)
We had a similar issue (and needed what sounds like the same script) relating to ArcGIS suddenly not liking shapefiles/rasters on a network drive, with the MXD/layers file crashing ArcMap/ArcCatalog instantly as soon as you tried to open them. This happened across multiple installations and versions, all on the one day, and has actually occurred twice now. It seemed as though ArcMap was corrupting something as it was writing to the document; possibly as a result of some network issue.
Crazily enough, the document was recoverable, by disconnecting the network adaptor or data source drive, then re-saving it - the new document worked absolutely fine, didn't even need to repair data connections. Disabling the data source also allowed the following script to work:
#-------------------------------------------------------------------------------
# Name: ExportMXDLayers.py
#
# Purpose: List the layers of a specified mxd and attempt to save layer
# files from the layers in that mxd
#
# Author: Peter von Minden and Julian Ward
# Edited: Andrew Quee
#
# Created: 22/08/2013
# Edited: 16/12/2013
#-------------------------------------------------------------------------------
import arcpy
errSummary = ""
succSummary = ""
# Must have a valid path to to the mxd in question using double backslashes "\\"
mxd = arcpy.mapping.MapDocument("D:\\Temp\\Export.mxd")
print "\nLAYER_NAME,DATA_SOURCE"
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
print lyr.name + "," + lyr.dataSource
try:
# Must have a valid folder to dump the .lyr files using double
# backslashes "\\"
arcpy.SaveToLayerFile_management(lyr, "D:\\Temp\\" + lyr.name + ".lyr", "ABSOLUTE")
succSummary += "Sucessfully Created " + lyr.name + ".lyr\n"
except:
errSummary += "Failed Creating " + lyr.name + ".lyr\n"
print "\n****SUMMARY FOR LAYER FILE CREATION****\n\n****SUCCESFUL****\n\n" + succSummary + "\n****ERRORS****\n\n" + errSummary
wait = raw_input("Press ENTER to continue")
(Sorry if it's a bit rough, it was literally hacked together in a few minutes while users were screaming about 'nothing working' to recreate their broken MXDs)
Coming back to the question, I'm not sure this is possible, David's tip aside. In our experience, as soon as any Esri application touches a corrupted MXD, bang "ArcGIS Desktop has encountered a serious application error and is unable to continue." This also applies to ArcPy, as you've found.