From the Catechism:
1514 The Anointing of the Sick “is not a sacrament for those only who are only at the point of death. Hence, as soon as anyone of the faithful begins to be in danger of death from sickness or old age, the fitting time for him to receive this sacrament has certainly already arrived.”
1515 If a sick person who received this anointing recovers his health, he can in the case of another grave illness receive this sacrament again. If during the same illness the person’s condition becomes more serious, the sacrament may be repeated. It is fitting to receive Anointing of the Sick just prior to a serious operation. The same holds for the elderly whose frailty becomes more pronounced.
1517 It is very fitting to celebrate it within the Eucharist, the memorial of the Lord’s Passover. If circumstances suggest it, the celebration of the sacrament can be preceded by the sacrament of Penance and followed by the sacrament of the Eucharist.
So, yes, you can receive this sacrament before surgery. If you are mobile, sometimes the priest will do it following a Mass after you have already received Holy Communion. If you are in the hospital or homebound, the priest will come to you and offer you reconciliation, anointing of the sick and the Holy Eucharist if you desire. You can also receive the sacrament if you suffer from a chronic illness or condition or if you suffer from addiction.
Here’s the really cool thing about this sacrament, again from the Catechism:
1521 Union with the passion of Christ. By the grace of this sacrament the sick person receives the strength and the gift of uniting himself more closely to Christ’s Passion. Suffering, a consequence of original sin, acquires a new meaning; it becomes a participation in the saving work of Jesus.
We can offer our sufferings to God every day, but with this sacrament they become especially sanctified!
God bless.