If I somehow locked myself out and my wife was out of town, then I would have to go to work to get my spare key, getting myself let into my office by the security guard.
But how I would handle it would depend on what time it was and which day of the week it was and whether I had any money with me and whether I had my cell phone, and a mix of other circumstances. For example if I had money and it was during shopping hours, I could go to the mall near where I worked and buy some really cheap pants and wash off any obvious makeup. Or I know a public bathroom downtown that is open until 1 AM. Or I could go to the GLBT club to clean up -- even if I didn't have my membership card with me, they would recognize me and let me in. There are usually numerous options if you stop to think about them.
Reminds me of the time [before I dressed] when I reached my then- girlfriend's apartment on the last train of the night when I was expected the next morning. It was after 1 AM and I had about the equivilent of 40 cents in local currency, and my girlfriend didn't hear me knocking on the door or ringing the bell; and it was winter (it would have been tough but possible to stay out all night.) So I went down to her storage area in the apartment basement and slept in her locker. It wasn't glamerous but I survived. Options if you think outside the box. (Or, in my case, it was a matter of thinking "inside the box")