How to check if an mxd is open?

786
1
Jump to solution
12-07-2017 04:31 AM
Zeke
by
Regular Contributor III

I have a script that adds layer files to a map and optionally saves it. If the map is open, I just reference the map like normal, i.e. mxd = arcpy.mapping.MapDocument("CURRENT").

I'd like to be able to check whether the map is open in the first place, so I can reference the map through a parameter if it isn't.

What I've come up with so far is to use a try/except block to catch the runtime error generated if the mxd is not open, then set it to a default if so.

Is there a way to check if the map is open or not? Thanks.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Try/except is fine, this is what happens when you do the equivalent in Pro, so I imagine that arcmap behaves the same.

try:
    aprx = arcpy.mp.ArcGISProject("CURRENT")
except:
    print("big failure")
    
big failure‍‍‍‍‍‍

View solution in original post

1 Reply
DanPatterson_Retired
MVP Emeritus

Try/except is fine, this is what happens when you do the equivalent in Pro, so I imagine that arcmap behaves the same.

try:
    aprx = arcpy.mp.ArcGISProject("CURRENT")
except:
    print("big failure")
    
big failure‍‍‍‍‍‍