Perturbation calculation of transition moments. Application to H2 and MgO