5

My question is similar to Adding Layer in current session using ARCPY . My script works in ArcMap python window but not as a script in toolbox. I am running ArcGIS 10.0 SP4. The script is given below, and the error says it fails at line 3, "CreateObject cannot open map document"

import arcpy
import arcpy.mapping as map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "SectionClip",df)[0]
arcpy.MakeFeatureLayer_management(updateLayer,"SectionInner2")
sourceLayer = arcpy.mapping.ListLayers(mxd,"SectionInner",df)[0]
newupdateLayer = arcpy.mapping.ListLayers(mxd, "SectionInner2",df)[0]
arcpy.ApplySymbologyFromLayer_management(newupdateLayer,sourceLayer)
newupdateLayer.showLabels = True
for lyr in map.ListLayers(mxd):
    if lyr.name == "SectionInner2":
        lyr.showLabels = True
        for lblclass in lyr.labelClasses:
            lblclass.expression = '"%s" & [SECTION] & "%s"' % ("<CLR red='0' green='0' blue='0'>""<FNT size='12'>""<BOL>", "</BOL>""</FNT>""</CLR>")
            lblclass.showClassLabels = True

arcpy.RefreshActiveView()
mxd.save()

I am unable to upgrade to ArcGIS 10.1 any time soon so the option of using a python add-in is not available.

4
  • 4
    Do you have the "Always run in foreground" and "Run Python script in process" options checked?
    – blah238
    Nov 2, 2012 at 0:21
  • 2
    I think the reason @blah238 asks his question is described well in the online help under the heading Reference a map document on disk or use the CURRENT keyword within ArcMap
    – PolyGeo
    Nov 2, 2012 at 4:58
  • Did @blah238 suggestion help you? If so, then let us know so that can be transferred to an Answer that you can Accept.
    – PolyGeo
    Nov 22, 2012 at 8:24
  • Checked both "Always run in foreground" and "Run Python script in process". Still not working. btw thanks PolyGeo for the link, helped me understand what the checks were for.
    – techtonic
    Nov 30, 2012 at 21:33

1 Answer 1

7

The only thing I see in your code that could cause this problem is that you are setting the MXD value to "CURRENT", which is fine... IF you have an MXD open (executing the script tool from within ArcMap).

I was able to cause your code to fail with the same response if I tried executing the code form the stand-alone ArcCatalog application. Is that what you were doing?

Screenshot using ArcCatalog to execute a Script Tool enter image description here



If trying to run the script from stand-alone ArcCatalog was your problem, you have two options to run your script.
1. Run the Script Tool from the Catalog tab within the ArcMap enter image description here

2. Application Set the MXD parameter as a path to a valid MXD

arcpy.mapping.MapDocument(r"c:\path\to\your\map.mxd")




You can read more details about the use of the arcpy.Mapping.MapDocument class here

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.