Error 000582 - "Export to PDF" causing problems with "Select by Attribute"??

Discussion created by mattsimonds on Mar 20, 2013
Latest reply on Mar 25, 2013 by mattsimonds
I've created a python script that manipulates a page layout so that I can export the view to a PDF.  The code uses several different ""Select By Attribute" methods.

My code operates as a function in a module I import into the arcpy window.  After executing the function a number of times (variable number - say between 5 and 20) the following error is returned
Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000582: Error occurred during execution.

The code section that causes the error is:
    ##Highlight correct SU in extent window
    df = arcpy.mapping.ListDataFrames(mxd, "Legend and Extent Rectangle")[0]
    for lyr in arcpy.mapping.ListLayers(mxd, bldg, df):
        for sublyr in lyr:
            if == s:
                sublyr.visible = True
                arcpy.SelectLayerByAttribute_management(sublyr, "NEW_SELECTION", "[SU] = %d" % (SU))
                sublyr.visible = False

Under geoprocessing options, I turned OFF background processing because simple selection like the one above take FOREVER with background processing turned on. (Side question - why must the map refresh after every selection?  Can I supress that??)

I noticed however that if I turn background processing ON, I can run my code again without a problem.  Does anyone have an idea what is causing this problem, and why it might be so seemingly random?