How to copy-paste heading and its content in MS word from Matlab?
Hi everyone, I’m working on a Matlab code that allows to write a report in MS word as follows (the following code has been written taking as reference Office VBA Reference and AI tools):
clc; clear all; close all;
% 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);
headings = doc.GetCrossReferenceItems(1) % here I get all the headings of my .docx
% Find the index of the heading you want to copy
heading_index = find(strcmp(headings, ‘ 2.1 TR-x-xxx’));
% Get the range of the heading and its content
start_range = doc.Paragraphs.Item(heading_index).Range.Start;
end_range = doc.Paragraphs.Item(heading_index + 1).Range.End; % End of the next paragraph after the heading
content_range = doc.Range(start_range, end_range);
% Copy the content
content_range.Copy;
Now I would like to copy an heading (TR-x-xxx) and its content (see the following image)
it follows as I would do in MS word if I didn’t used Matlab:
Once selcted and copied, I would like to get the following result (I would like to be able to copy-paste and get as many headings+content as I want):
Is my code right? How do i proceed??Hi everyone, I’m working on a Matlab code that allows to write a report in MS word as follows (the following code has been written taking as reference Office VBA Reference and AI tools):
clc; clear all; close all;
% 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);
headings = doc.GetCrossReferenceItems(1) % here I get all the headings of my .docx
% Find the index of the heading you want to copy
heading_index = find(strcmp(headings, ‘ 2.1 TR-x-xxx’));
% Get the range of the heading and its content
start_range = doc.Paragraphs.Item(heading_index).Range.Start;
end_range = doc.Paragraphs.Item(heading_index + 1).Range.End; % End of the next paragraph after the heading
content_range = doc.Range(start_range, end_range);
% Copy the content
content_range.Copy;
Now I would like to copy an heading (TR-x-xxx) and its content (see the following image)
it follows as I would do in MS word if I didn’t used Matlab:
Once selcted and copied, I would like to get the following result (I would like to be able to copy-paste and get as many headings+content as I want):
Is my code right? How do i proceed?? Hi everyone, I’m working on a Matlab code that allows to write a report in MS word as follows (the following code has been written taking as reference Office VBA Reference and AI tools):
clc; clear all; close all;
% 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);
headings = doc.GetCrossReferenceItems(1) % here I get all the headings of my .docx
% Find the index of the heading you want to copy
heading_index = find(strcmp(headings, ‘ 2.1 TR-x-xxx’));
% Get the range of the heading and its content
start_range = doc.Paragraphs.Item(heading_index).Range.Start;
end_range = doc.Paragraphs.Item(heading_index + 1).Range.End; % End of the next paragraph after the heading
content_range = doc.Range(start_range, end_range);
% Copy the content
content_range.Copy;
Now I would like to copy an heading (TR-x-xxx) and its content (see the following image)
it follows as I would do in MS word if I didn’t used Matlab:
Once selcted and copied, I would like to get the following result (I would like to be able to copy-paste and get as many headings+content as I want):
Is my code right? How do i proceed?? actxserver, word, vba MATLAB Answers — New Questions