Clear Filters
Clear Filters

How move selection through a .docx document from Matlab?

6 views (last 30 days)
Hello everyone, I've to write a report in MS word from Matlab and I'm using actxserver('Word.Application') and, despite of some material found on the internet, I can't move the selection to a given point of my document. I load a template .docx I subsequently need to modify and interact with.
Here is my MWE (it doesn't work since I am not allowed to upload .docx file)
% Template path
tmpl_path = 'C:\Users\user_name\Desktop\template_report.docx'
% Create a new Word document from the template
word = actxserver('Word.Application'); % it's a Matlab function
word.visible=1;
doc = word.Documents.Add(tmpl_path);
Tried to use the following command based on this documentation
word.Selection.GoTo(4, 1, [], [])
In the command window I get "ans = Interface.0002095E_0000_0000_C000_000000000046". But in the .docx window the selection is standstill at the beginning of the document
while I need to move it at the beginning of the second section.

Answers (2)

Mario Malic
Mario Malic on 10 May 2024
You have mistaken WdGoToItem enumeration, number 4 is for footnote. You need 0 or 11.

Giuseppe
Giuseppe on 13 May 2024
I managed to solve the issue in different ways:
  1. Method word.Selection.GoTo(1, 1, 4);
  2. Customized function to move selection after a disired word.
%% 1 - Method: word.Selection.GoTo(What, Which, Count, Name)
% Description: move selection to a specific point of the document
% Parameters:
% What: Specifies the type of object to navigate to.
% 1: Navigate based on a numeric position within the document.
% 2: Navigate based on a bookmark within the document.
% 3: Navigate based on a comment within the document.
% 4: Navigate based on a section within the document.
% 5: Navigate based on a table within the document.
% 6: Navigate based on a graphic within the document.
% 7: Navigate based on an equation within the document.
% Which: Specifies the direction of navigation relative to the object specified by What.
% 0: Move to the end of the object.
% 1: Move to the beginning of the object.
% Count: Specifies a count or index that further refines the navigation. The interpretation of this parameter depends on the value of What.
% Name: Specifies additional criteria for the navigation.
% For numeric position (What = 1), Name indicates whether to start from the beginning (1), end (0), or specified character (2).
% For section navigation (What = 4), Name indicates whether to start from the beginning (1), end (0), or first page (2) of the specified section
%% 2 - Customized function
function move_sel(wrd)
% Function to move selection after a desired word
global word;
% Use the Find method to locate the word "Section":
findSec = word.Selection.Find;
findSec.Text = wrd;
findSec.Execute;
if findSec.Found
word.Selection.MoveRight; % Move the cursor to the right of "Section"
word.Selection.TypeParagraph; % Move the cursor to a new line
word.Selection.Style = word.ActiveDocument.Styles.Item('Normal'); % set the desired style
end
end

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!