CIC
Canon 996
Book IV. The sanctifying function of the Church
Sacraments
Code of Canon Law
English§1. To be capable of gaining indulgences, a person must be baptized, not excommunicated, and in the state of grace at least at the end of the prescribed works.
§2. To gain indulgences, however, a capable subject must have at least the general intention of acquiring them and must fulfill the enjoined works in the established time and the proper method, according to the tenor of the grant.