4

This should be a really simple process but I am having issues. I want to search a workspace for all files with a certain string and then add them to the current workspace with a selected symbology.

Issues:

  1. Error [[: Object: CreateObject cannot open map document]] I have to set the map doc to be the current workspace
  2. When run in the python window the symbology is not updated.
  3. It should only update the map frame/toc once everything is added

Code:

import os, arcpy, arcpy.mapping
from arcpy import env

GDB = arcpy.GetParameterAsText(0)
Text = arcpy.GetParameterAsText(1)
symbologyLayer = arcpy.GetParameterAsText(2)
#OutputGDB = arcpy.GetParameterAsText(3)

arcpy.env.workspace=GDB
mxd=arcpy.mapping.MapDocument("CURRENT")
list=arcpy.ListFeatureClasses('*'+Text+'*')

for layer in list:
    dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    addLayer = arcpy.mapping.Layer(layer)
    arcpy.mapping.AddLayer(dataFrame, addLayer, "BOTTOM")
    arcpy.ApplySymbologyFromLayer_management (addLayer, symbologyLayer)

# Clean up the MapDocument object by deleting it
#del mxd

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

ERROR DETAILS

Messages

Executing: ProcessDatasets22 P:\2012\Job_044_DM_Radio_Propogation\Working\FinalPropogation\cb_BEL07\working\bel07.gdb _dow P:\2012\Job_044_DM_Radio_Propogation\Working\signal_strength_poly.lyr Start Time: Mon Mar 19 14:11:27 2012 Running script ProcessDatasets22... : Object: CreateObject cannot open map document Failed to execute (ProcessDatasets22). Failed at Mon Mar 19 14:11:27 2012 (Elapsed Time: 0.00 seconds)

2 Answers 2

9

found the answer -it has to run in the foreground and can not be in the background or from a catalog session.

2

I think the problem may be that you need the keyword for the current workspace to be set to "CURRENT" (as per the documentation's example code) not "Current".

Also, as an aside you appear to have this line of code (still needs CURRENT to be put in) twice:

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

Something else that seems astray is that you are treating your list of feature classes as if they are layers. For example, the Layer object expects layers from either an *.mxd or a *.lyr but you are feeding it feature classes. I'm thinking that you should use MakeFeatureLayer_management on your feature classes first.

7
  • nope...still get the same issue. See updated code in question. Having mxd twice was a mistake.
    – GeorgeC
    Commented Mar 19, 2012 at 4:14
  • Try running the mxd=arcpy.mapping.MapDocument("CURRENT") line in the Python window of ArcMap where you should see that no error is thrown. Then do the same outside of ArcMap (I just did it from IDLE) and even though you will need to "import arcpy" first doing it that way, it will always throw the error you see, because without ArcMap it won't know about the current map. If this does not work, perhaps try getting your code working with variables hardcoded first.
    – PolyGeo
    Commented Mar 19, 2012 at 6:30
  • It does work fine when in the python shell (argis/idle using hard coded fields) but doesn't when it is run from a tool. As I need to use it often, I need it to work as a tool. It is weird as other tools (like text replacement) work fine with the same mxd="CURRENT" line. Possibly time to ask in esri forums
    – GeorgeC
    Commented Mar 19, 2012 at 22:37
  • Is it still throwing an error or just not saving the added layers? I ask the latter because no mxd.save() is present in the code above. If you've now included MakeFeatureLayer, you may want to update your code above with its latest state and current error/symptom. I usually ask ArcGIS questions in that forum first then post here if no resolution/progress within a day or so.
    – PolyGeo
    Commented Mar 19, 2012 at 22:46
  • If it works fine from Python window of ArcMap and from IDLE, but not from tool, then perhaps look at the data types on your tool parameters, because you are reading them all into the Python script as text strings.
    – PolyGeo
    Commented Mar 19, 2012 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.