Are you using Visual Studio? If so, then you should just cut and paste it from the VS-IDE into the word. Because the VS-IDE Copy code is in a rich text form, Word will retain the formatting when it is pasted. I do this all the time (including with SQL) and it works much better than anything else that I came with.
(If you are not using Visual Studio, then I'm sorry, I do not know.)