Two key things to confirm, and based on the results you'll know what to do next:
1) are you getting +12V at the light? Remove the housing assembly from the roof and unplug it. With a volt meter confirm that there is a constant +12V at the "on" wire and when the door courtesy switch is extended out that the "door" wire gets +12V. From looking at the lamp and its 3 position switch you should be able to determine which wire is which.
2) If those two check out, and the vehicle IS supplying power to both positions, then I bet the ground for the sunroof has been lost. This used to be an issue on Toyotas with sunroofs, sometimes. At some point a dedicated ground wire was added to the dome light harness (maybe at Gen5, I forget). To fix it, you want to pull the edge of the headliner enough to access one of the sunroof mounting bolts, and loosen and tighten it until the ground is restored enough for the dome light to work again.
3) If 1) does not show +12V at the "door" wire, then I'd suspect the courtesy switch in the B pillar, like DF Bonnett suggested in his 1st post, they get corrosion and don't make contact to ground as they should do. Sometimes jiggling it can get it to work, periodically. A new switch would restore operation, but access to the wire harness connector requires removal of several pieces of trim (later generations moved this wire harness connector closer, so it could be replaced more easily. Some have found that disassembly and cleaning was enough to restore the courtesy switch operation at the B Pillar.