> I tell him off when he behaves like this, but it doesn't seem to do anything
Even telling him off can re-enforce the behaviour, better to completely ignore him or to give him a task for which he can be rewarded. ie, get him to lay down & wait, after a little hwile give him a treat for doing that.
My dog is a foodie and will try his hardest to get a treat out of us. If he is trying in a good way, (sitting nicely, laying down, or closing cupboard doors/draws) then he'll get his treat, but if he is pestering (pulling my OH to the treat cupboard by the seat of his pants, pushing my OH to the treat cupboard, barking, pulling trousers legs, pawing etc.) then he gets ignored.