Also the A330 and A340 share the same wings, so the only difference is really the engines.
The normal operations ranges of these aircraft are:
A332: 6650 nm
A333: 5600 nm
A342: 8000 nm
A343: 7300 nm
So, as mentioned above, the A330 is suited also for long haul (Europe to west-coast USA e.g.), and many airlines use it for long hauls (LH, NW
I think the reason why it's often used for medium routes is the high amount of passengers it carries and the low costs/seat/mile on those routes in comparison to other aircraft. Many charter airlines use the A330 on such routes.
also uses the A343 on medium routes to the middle east for example.
ETOPS, as already said, is probably a huge factor for airlines to use "4 engines 4 long haul", but then, the 772 flies on ultra long hauls...
Maybe also the fact that 2 engines are cheaper in maintenance than 4 engines may convince airlines to use the A330 instead of the A340...
Why is 10 afraid of 7? Because 7 8 9.