Yo lo tengo un poco más simplificado:
Código Delphi
[-]
function LetraNIF( NIF:integer): Char;
const S = 'TRWAGMYFPDXBNJZSQVHLCKET';
begin
result := S[(NIF mod 23)+1];
end;
y la llamada habitual:
Código Delphi
[-]
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage( LetraNIF(12345678) );
end;