A sermon of Caesar of Arles (ca. A.D. 470-542) contains the following: “As often as some infirmity overtakes a man, let him who is ill receive the body and blood of Christ; let him humbly and in faith ask the presbyters for blessed oil, to anoint his body, so that what was written may be fulfilled in him: ‘Is anyone among you sick? Let him bring in the presbyters, and let them pray over him, anointing him with oil; and the prayer of faith will save the sick man, and the Lord will raise him up; and if he be in sins, they will be forgiven him. . . . See to it, brethren, that whoever is ill hasten to the church, both that he may receive health of body and will merit to obtain the forgiveness of his sins” (Sermons 13:3).
from the CA site: catholic.com/library/Anointing_of_the_Sick.asp
…Canon 1007 excludes the giving of the sacrament to those who are manifestly unrepentant. If the person is still conscious the way to the state of grace is through the Sacrament of Penance, not through Anointing of the Sick. Having repented, they can then be anointed. If the sick person is unconscious and is known to have obstinately persisted in grave sin up to the point of losing consciousness, with no sign of repentance, they cannot be anointed. However, this is a high bar for denying the sacrament. Such a person who showed even an implicit sign of repentance (e.g. “please call the priest”), could be anointed. Another person who while not an obstinate sinner was nonetheless in the state of grave sin, but who had manifested an habitual desire to die a Catholic, could be anointed, even if he became unconscious in the very act of sinning. The basis of the different treatment is a prudent judgment that given their habitual frame of mind the person would repent if he could.
from a good summary about the sacrament of the Anointing of the Sick: