  Let $M\subset B(\mathcal H)$ be a von Neumann algebra acting on the Hilbert
space $\mathcal H$. We prove that $M$ is finite if and only if, for every $x\in
M$ and for all vectors $\xi,\eta\in\mathcal H$, the coefficient function
$u\mapsto \langle uxu^*\xi|\eta\rangle$ is weakly almost periodic on the
topological group $U_M$ of unitaries in $M$ (equipped with the weak or strong
operator topology). The main device is the unique invariant mean on the
$C^*$-algebra $\operatorname{WAP}(U_M)$ of weakly almost periodic functions on
$U_M$. Next, we prove that every coefficient function $u\mapsto \langle
uxu^*\xi|\eta\rangle$ is almost periodic if and only if $M$ is a direct sum of
a diffuse, abelian von Neumann algebra and finite-dimensional factors.
Incidentally, we prove that if $M$ is a diffuse von Neumann algebra, then its
unitary group is minimally almost periodic.
