Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm having trouble using a code block in the Field Calculator with ArcObjects. Ideally, I'd like to be able to read a text file which contains the Python code for the code block. I've tested this code before in ArcMap and am sure it works. However, ArcMap keeps crashing when I try to do this. Stepping through the code, it crashes at the line that executes the tool.

I also tried a simpler code block without reading from a text file and it still crashed at the same step (executing the tool). I've included my code below. If anyone can point me in the right direction, I'd really appreciate it!

    'Create new geoprocessor
    Dim GP As IGeoProcessor2 = New GeoProcessor
    GP.AddOutputsToMap = True
    GP.OverwriteOutput = True

    'Read the text file containing the python code for the code block
    Dim codeBlock As String
    Dim sr As New StreamReader("path to text file")

    codeBlock = sr.ReadToEnd()

    'Using a simpler code block:
    'codeBlock = "def test():" & vbLf & "  out = ""test""" & vbLf & "  return out"

    'Add the parameters into array
    Dim parameters As IVariantArray = New VarArray
    parameters.Add("path to feature class")          'Feature class
    parameters.Add("RepStyle")                       'Field name
    parameters.Add("trans( !CODE!, !STYLE! )")       'Expression
    parameters.Add("PYTHON_9.3")                     'Expression type
    parameters.Add(codeBlock)                        'Code block

    'Run the field calculator
    GP.Execute("CalculateField_management", parameters, Nothing)
share|improve this question
trans(!CODE!, !STYLE!) does not match test(), try parameters.Add("test()")... the line that's commented, and the simplified code block is also commented out; as it stands the problem could be in the text file, which would need to be listed in the question. I'm assuming the first parameter "path to the feature class" is actually a path to a feature class in your code and the RepStyle field exists in this feature class. –  Michael Miles-Stimson May 11 at 1:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.