Masturbating in order to procure sperm is still masturbation. But the moral problem in masturbation is that the act is deficient in human goods. The sex act is unitive and procreative, but masturbation is neither unitive nor procreative. Usually, masturbation is motivated solely by sensible goods (the feeling of orgasm). The feeling of orgasm is not evil in itself. Rather, the trivialization of the sex act is where the evil or deficiency lies. The action lacks full human goodness. To willingly carry out an action that is deficient in intelligible human goods, that is, in human goods that by nature belong to it, is to willingly do evil.
A simple way of clarifying this is to distinguish between three elements of the human action: Moral Object, Motive, and Circumstances.
In order for an action to be good, all the elements have to be good. If one element is evil, the entire act is evil (since evil is a deficiency). So, a good act done for a bad motive is deficient (i.e., helping an old lady cross the street just for money). A bad act done for a good motive is still deficient (murdering a career criminal to spare society future costs). A good act (target practice with a 44) done with a good motive (in order to improve one's shot) can be overall a bad action in the wrong circumstances (a crowded bus station). The circumstances render the act dangerous to others and thus irresponsible.
The action we are looking at is masturbation in order to procure sperm, which will later be used to bring about a conception with one's wife's oocyte in a petri dish, in order to have a child. The ultimate motive is having a child. Ultimately, this is a good motive. But getting sperm via masturbation still involves masturbation. The circumstances do not change the nature of the act of masturbation. One is still engaging in solitary sex. The act still lacks the unitive aspect as well as the procreative aspect. The action is still deficient. It is still a trivialization of the sexual act. The moral object is still masturbation.
Granted, the motive is not merely to procure pleasure. Nevertheless, it is a utilitarian act. Sex, in this case, has become utilitarian. But the sex act, in order to be humanly good, must be carried out for its own sake, not for the sake of extraneous purposes. Human goods are pursued for their own sake, not for the sake of some other end. Masturbation as a means of procuring sperm in itself does not serve intelligible human goods. The proof is its utilitarian nature in this case. The sperm, once procured, will then be taken to some other location and brought into contact with various oocytes. But that does not make the previous act of masturbation a procreative action, much less a unitive action.
It is true that with regard to some actions, circumstances can alter the nature of the action, that is, alter the moral object. For example, shooting a gun can be murderous, or it can be self-defensive. Or, consider taking the pill to regulate a cycle. This has the side effect of infertility. One does not take the pill, in this case, to intentionally render the sex act infertile. Hence, taking that pill in this case is not contraceptive behaviour. The moral object, that is, what is being done, is cycle regulation. In shooting the violent aggressor who has a gun to the head of the hostage, what the marksman is doing (moral object) is stopping and disarming the aggressor. His death is a side effect of his action.
Masturbating in order to procure sperm, though, does not work like these cases. What is being done? In other words, what is the moral object? Masturbation, which involves willingly engaging in an action deficient in intelligible human goods. What is the motive? To bring about a conception "in glass", or to measure sperm count. The deficiency in masturbatory acts is not a side effect of some other action. It is there still, as part of the very action of masturbation.
It is comparable to adultery for a good motive (although it is far less serious than adultery). Committing adultery in order to save one's life or another's life is still adultery. The circumstances have not changed the nature of the act. Imagine we are in 15th century England, and our village is being raided. A man enters your hut and is about to kill your husband. But you say: "Don't! I will voluntarily sleep with you if you spare my husband". By choosing to lay with the man in order to save your husband, you are choosing adultery, but for a good end. The moral object is still adultery, and it is still doing evil to achieve good. The circumstances have not altered the nature of the action. So too, the motive of an in vitro conception does not change the nature of the act of masturbation.