He gets anxious when he can't get to you. The anxiety causes stomach upset, so he poops/pees to relieve it.
Have you tried distracting him with something (like a filled kong, maybe, or an interactive toy or something yummy to chew on)? Something to keep him busy while you're out of his sight? This might manage his behavior.
To try to eliminate the behavior, though, it sounds like he might need some behavior modification in this area. Maybe something like shutting yourself out of sight for a few seconds at a time, then a half a minute, then a minute, then two minutes, you get the idea. Keep your leaving and coming back low key, until he gets the idea that yes, you leave, but yes, you come back.
Crating him for these periods may also be a management option.
BTW, the title of your thread made me LOL. I know it's not a funny situation, but I couldn't help myself!