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() sr.Close() '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("test()") parameters.Add("PYTHON_9.3") 'Expression type parameters.Add(codeBlock) 'Code block 'Run the field calculator GP.Execute("CalculateField_management", parameters, Nothing)