You can also supplement training with physical barriers as necessary. :)
My son is in daycare most of the time, so it's not a large problem. But the few times he's been home while I have been working, the major issue with closing the door is he knows I'm still in here. So he'll make a bunch of noise, yelling for me to help him. It's often easier to just fix his Duplo/juice box/etc issue.
It really depends on the age, I'm struggling to get my child to poop in the toilet, I'm not sure he's ready to learn not to barge into the office when he knows his dad is in there.