Expanding foam is too messy and only causes problems.
heat/cool loss comes more from windows, walls, ceilings, doors, and duct vents. I would tackle them first and leave the base boards for later.
If you are just dead set on doing this project. Base boards are notorious for splitting when removing them. The milling creates different thickness and sometimes the nail hits the thinnest spot and when you pull it off it splits. Now again if dead set on this trivia project, go very slowly and get two chissles and walk the board off by slowly prying one area, then pry it more then stick the other chissel up front of this one and pry it then get the back chissle and stick it in front of the front one and walk it off.
Now, even me, when I install a door kit and try dillegently to salvage the existing door trim I still break half the trim I take off thats why I always state up front that the trim will have to be replaced.
Could you not consider a clear caulking that you can apply with everything in tact? This would work and would be much easier. Expanding foam causes pressure problems and even if you continued doing this and sprayed the foam, the molding would probably not go back on due to the expanding foam.