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.