The first solution that comes to my mind is to setup your template like this:
1)Pull account info from file
2)Clear cache/cookies
3)Login with account info
4)Setup a counter to let the template remember how many comments it has done - start counter at 0
5)Pull random link from file and comment on it
6)Increase counter by 1
7)Check if the counter is less than 40
8a)If step 7 returns a true value then go back to step 5
8b)If step 7 returns a false value, then all 40 comments are done - Template completed successfully
I haven't messed much with counters in ZP, but I think you should be able to set up a loop that works something like this. I haven't used it but I see the counter function in the macros, it should be as easy as using the Init counter macro to start, then with each loop the Increase counter value macro, and finally use a logic check with the Get counter value macro to see if it is less than 40. If it is less than 40, connect the green dot with the blue dot and you're good to go
That's what I would expect to work anyway - as I said I haven't personally used those macros yet. But give it a shot!