Yes. A common misconception among pet owners is that if their animal lives exclusively indoors they do not need heartworm prevention medication. Heartworm disease is primarily spread through mosquito bites and mosquitos can enter the house through open windows or as you are entering or exiting the home.
In addition, even if your pet lives indoors, it is possible that they may escape through a door accidentally left open or as you are entering or exiting the home.