I have an ArcMap 10.5 ModelBuilder model that contains within it a script tool referencing the 'CURRENT' mxd. Both the script tool and the ModelBuilder tool are set to run in the foreground. The script tool runs fine outside of ModelBuilder, but the ModelBuilder model that contains the script tool throws a runtime error at

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

The error message is:

Traceback (most recent call last):
File "T:\OSPR_Data\Ospr_Tools\GIS\ArcGIS\OSPR\Utilities\scripts\Seth\FlowModel 18 TBX.py", line 661, in <module>
File "T:\OSPR_Data\Ospr_Tools\GIS\ArcGIS\OSPR\Utilities\scripts\Seth\FlowModel 18 TBX.py", line 657, in main
File "T:\OSPR_Data\Ospr_Tools\GIS\ArcGIS\OSPR\Utilities\scripts\Seth\FlowModel 18 TBX.py", line 556, in Part4
mxd = arcpy.mapping.MapDocument("CURRENT")
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\arcobjects\mixins.py", line 652, in __init__
super(MapDocumentMethods, self).__init__(mxd)
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__
for arg in args))
RuntimeError: Object: CreateObject cannot open map document

Is this a bug, or is there a way to reference the 'CURRENT' mxd in a script within ModelBuilder?

  • Is the Python script tool also set to run in process? i.e. not just in foreground - see gis.stackexchange.com/q/61158/115 – PolyGeo Mar 29 '17 at 21:15
  • Is the model tool also set to run in process and in the foreground? – PolyGeo Mar 29 '17 at 21:17
  • Yes, the box to run the arcpy script in process is checked. – spaine Mar 29 '17 at 21:22
  • I don't see a checkbox in the model properties to run it in process. – spaine Mar 29 '17 at 21:24
  • I'm not at a laptop at the moment but I'll try to run a quick test soon. In any event, any time I want to work the the current map I do that using a Python AddIn. – PolyGeo Mar 29 '17 at 21:28

To try and reproduce what you describe I did the following:

  1. Wrote C:\Temp\test.py

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
arcpy.AddMessage("Using CURRENT map now")
  1. Right-click on a toolbox in the Catalog window and used Add Script to make a Python script tool from the above script. I took care to tick Always run in foreground and to tick Run Python script in process
  2. Tested the above tool and got:

Start Time: Thu Mar 30 09:20:24 2017
Running script test...
Using CURRENT map now
Completed script test...
Succeeded at Thu Mar 30 09:20:24 2017 (Elapsed Time: 0.04 seconds)
  1. Dragged test tool into a newly created model. The model has to Always run in foreground because that checkbox was greyed out when I looked at it. There is no choice to make about Run Python script in process because that option is not present. Saved and closed the Model.

enter image description here

  1. Double-clicked to open and then run the Model. The output was:

Executing: Model
Start Time: Thu Mar 30 09:25:46 2017
Executing (test): test
Start Time: Thu Mar 30 09:25:46 2017
Running script test...
Using CURRENT map now
Completed script test...
Succeeded at Thu Mar 30 09:25:46 2017 (Elapsed Time: 0.03 seconds)
Succeeded at Thu Mar 30 09:25:46 2017 (Elapsed Time: 0.04 seconds)

As you can see the Python script tool within the Model seems to be able to create a map document object from the CURRENT map.

  • I tried running your test script + model in my MXD, and it failed. Then opened a blank MXD and tried it again, and it worked. So I tried running my model in a blank MXD, and it did not throw the error, but neither did it add the layers to the dataframe as it should have. So I'm not quite sure where the problem lies. – spaine Mar 30 '17 at 17:45

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.