use Win32::OLE; my $i = 0; while (1) { my $excel = Win32::OLE->GetActiveObject("Excel.Application"); last unless $excel; print "Found an excel\n"; last if ++$i > 10; print "Let's see ($i)\n"; }