The error 99 is a very generic "I don't know what went wrong" kind of error, but you mentioned that it happens after changing lenses. If the lens is an older lens that is not Canon (Sigma, etc...) it might not know how to correctly communicate with the 40D. I've heard of this specifically with some Sigma lenses that required re-chipping to make them compatible with newer Canon bodies.
Other than that, you may try cleaning the contacts where the lens meets the camera on the lens. If the 40D is new, the dirty contacts are probably on the lens side of the equation.