You must stay as neutral as possible when your dog does something ‘bad’, otherwise they might think that this behaviour is a good way to get your attention. Dogs don’t know the difference between good or bad, they just think “when I do this, my owner pays attention to me”, whether it’s a positive or negative interaction. You must reward every good behaviour. If your dog has been doing this for a long time, it will take him longer to lose the habit. If he does this because he wants to go outside, try to pick up on other signs that he wants to go out (he stands or sits by the door, for example). Personally I would recommend placing a little bell on the door. Every time he touches it, reward him and open the door. He should get it pretty quickly.