I haven't tried this myself, but here is something I found somewhere here on my travels:
How to manually code your remote function on your key:
If you have a dead battery and your key has forgotten the code when you replace your battery this info will save you a trip to the dealer and parting...