How to copy-paste heading and its content in MS word from Matlab?
Mostra commenti meno recenti
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??
Edit
I've updated the last image since I forgot a detail: I want to put the copies before the section "Summary" and not at the end of my .docx
N.B. My document contains a Table of Contents with the " TR-x-xxx " string. This is an important detail since if the string search starts from the beginning of the document, I need to start the selection from the second occurrence.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Software Development in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!