MATLAB For Beginners: 20-Minute Video Training Course. (Grab The Training Course and The Cheat Sheet for FREE)
Most likely, you will want to use a Word template to write data into a Word file. However, if you choose to do this, bear in mind that the cursor will start at the very beginning of your document when you open it.
Hence, you need to know how to navigate through the file and place your data in the proper location. Using the following function, you will get to choose:
- The direction to move your cursor towards (“up”, “down”, “right”, or “left”)
- The number of times to move toward this direction
function navigateWord(fid, direction, movementNumber) word = fid.actx; % set cursor selection = word.Selection; if strcmp(direction, 'down') selection.MoveDown(5, movementNumber); elseif strcmp(direction, 'up') selection.MoveUp(5, movementNumber); elseif strcmp(direction, 'right') selection.MoveRight(1, movementNumber); elseif strcmp(direction, 'left') selection.MoveLeft(1, movementNumber); end return
If you want to write data in a particular location, the way to know if you are at the desired location is by trial and error, unfortunately. You can try to figure out approximately the number of “down” movement by counting in your Word document the number of times you have to go down from the beginning of the document, but the most efficient way is to try to write something in a place and see the end result.
Another way to do this would have been to insert a dedicated area in your Word document and to fill it sequentially. However, this approach implies that you have control over the Word file prior to its generation. If you do not have control over your Word file (e.g. because it is a Word file locked in a source code control system that you cannot modify), you have to add the areas in your Word document manually every time you want to write data in it, which defeats the purpose of automating the generation of your document.