フィルターのクリア

How to copy-paste heading and its content in MS word from Matlab?

23 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2024 年 5 月 13 日
編集済み: Giuseppe 2024 年 5 月 15 日
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.

採用された回答

Zinea
Zinea 2024 年 5 月 15 日
The job here has two parts: first,  copying the necessary content under a specific heading, and second, pasting the copied content as many times as needed (assumed to be stored in “num_times”).
Copying content under a specific heading:
  • The code finds the "end_range,” which is the point until where the content is to be copied. The “start_range” is the “headingText” itself.
  • The structure of the document is to be understood for this copy operation. It is assumed that the “headingtext” has the style “Heading 1”.
  • The content from “start_range” to “end_range” is copied into the clipboard.
Pasting the copied content multiple times:
  • To determine the paste location, the “end_range” is calculated as the position of the last content on the page.
  • The cursor is moved to the end of the document after every paste operation.
Here is the MATLAB code for your reference:
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;
tmpl_path = 'C:\Users\zineadas\Documents\try.docx'; % Define your template path (update this path)
doc = word.Documents.Open(tmpl_path); % Open the document
headings = doc.GetCrossReferenceItems(1); % Get all the headings of my .docx
% Convert the headings to a MATLAB cell array of strings for easier handling
headingsArray = cell(headings);
% Find the index of the heading you want to copy
headingText = '2.1 TR-x-xxx'; % Define the heading text you are looking for
heading_index = find(strcmp(headingsArray, headingText));
if isempty(heading_index)
error('Heading not found');
end
% Assuming you have identified the heading_index as before
start_range = doc.Paragraphs.Item(heading_index).Range.End;
% Find the end_range
for i = heading_index+1:length(headingsArray)
currentStyle = doc.Paragraphs.Item(i).Range.Style;
% Check if the current paragraph style matches your heading's style
if currentStyle == 'Heading 1' % Adjust based on your specific heading level
end_range = doc.Paragraphs.Item(i).Range.Start - 1;
break;
end
% If no matching next heading is found, use the document's end
end_range = doc.Content.End;
end
% Now, end_range should correctly encompass all subheadings and content
content_range = doc.Range(start_range, end_range);
content_range.Copy;
% 'num_times' is the number of times you want to paste the copied content.
num_times = 5;
for i = 1:num_times
% Assuming 'end_range' is the ending position of your copied content.
% First, ensure 'end_range' is an integer and within the document's content length
end_range = max(1, min(end_range, doc.Content.End - 1));
% Use the range object to insert a paragraph after the specified range
rangeObj = doc.Range(end_range, end_range);
rangeObj.InsertParagraphAfter();
% Now, move the selection to the end of the document to ensure we're pasting at the correct location
word.Selection.EndKey(6); % Moves the cursor to the end of the document
% Paste the content that was previously copied to the clipboard
word.Selection.Paste();
end
doc.Save();
NOTE: The above code copies the content and pastes it directly with no changes to the indexing of the headings. In case you want the headings to follow a serial order, you need to use regexp” to print the headings separately, and use the above script to paste the content. Here is the link to the documentation for “regexp for your reference: https://www.mathworks.com/help/matlab/ref/regexp.html
Hope it helps!
  1 件のコメント
Giuseppe
Giuseppe 2024 年 5 月 15 日
Hi @Zinea, thank you so much for your detailed answer. Please, consider my updated question since I forgot to include some importat details. Howevere I am going to study your solution, stay tuned!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by