HM is right on timing.
Worming is often misunderstood. Worming is NOT a prevention, it merely kills any worms inside the pig (typically lung and intestine). Worming is also not 100%.
So by worming a sow pre-farrowing she will not be passing any protection on. However worm eggs are present in the dung which the young piglets will inevitably eat fragments of by mistake as they feed around their mothers feet at a few weeks of age. Hence worming the sow pre-farrowing will reduce the likelihood of the piglets picking up worms as she should be almost free of them. However as I said above it is not 100%, so you should worm your weaners at weaning as well to kill 99% of any they have picked up.