make sure that you check if the Target is actually containing a valid class value.
If Assigned(Target) then if (Target as TShape).name = 'xxxx' then
begin
end;
or better yet:
If target is TShape then if TShape(Target).name = '' then
begin
end;
or the easy way out (as first statement in your function):
if not assigned(Target) then exit