As long as you buy before the ex-dividend, and sell afterwards, you get the dividend and take the corresponding loss. Even if you held the stock only for an afternoon and morning.

Because of bookkeeping getting confused, the dividend might get sent to someone else who then sends it to you, but you are the one who gets the dividend and takes the capital loss.

Cheers,
Ben