One greatest way (better to publish) is to use the LaTEX instead Offices suite e use this library http://www.howtotex.com/tips-tricks/how-to-include-matlab-code-in-latex-documents/.
Other way, with Microsoft Word, is to use the Matlab Notebook resource or in the new Matlab versions the live-script https://www.mathworks.com/help/matlab/matlab_prog/what-is-a-live-script.html