Usually, you will be excommunicated if you attack the pope in anyway (as long as, of course, you're playing as a Catholic faction). It doesn't matter whether the pope attacked you first; he'll excommunicate you if you launch an attack against him, even if all you're doing is relieving a siege of your own castle. Thus, if you're playing as a Catholic faction, I'm not sure why you aren't excommunicated already.

However, if any of the Pope's territories turn rebel, you can occupy them without fear of excommunication. (He certainly doesn't remember what's supposed to be his.) It's aggression, not occupation, that aggravates the Pope.