Word ActiveX Delete content underneath a heading

4 次查看(过去 30 天)
Hi,
I am using word to store images of some measurament.
everything works fine but I would like to add some automation.
By now I am doing the following:
actx_word = actxserver('Word.Application');
actx_word.Visible = true;
trace(actx_word.Visible);
% Open existing document
word_handle = invoke(actx_word.Documents,'Open',fullfile(word_file_p));
%Here I would like to delete the content of the heading
Nr=2
WordUtils.WordGoTo(actx_word, 11, 1, Nr)%Go to specific heading in document
WordUtils.WordGoTo(actx_word_p, 3, 2, Nr)%Go one line down
hgexport(fig, '-clipboard')%Copy figure image to Clipboard
invoke(actx_word.Selection,'Paste');%Paste it to word document
actx_word_p.Selection.TypeParagraph; %enter
Now my Problem is I would like to rewrite the content of the heading. So delete first the content underneath it. Is there a easy way for doing that ?

采纳的回答

Thomas Jensen
Thomas Jensen 2021-5-10
Hi Alessandro,
Working with Word documents in MATLAB is not that easy indeed, even a simple task as the one you described might be a nightmare.
I am not used to the WordUtils functions, but that is how I would approach this issue:
You can get the objects returned by the ActiveX Library, for example, to open the document you can use the line:
wordDocument = actx_word.Documents.Open(fullfile(word_file_p));
If you know the text of the heading and it is unique int the document, you can search for the text and replace it by a different text:
wordRange = wordDocument.Content;
wordRange.Find.Execute('HEADING_TEXT');
wordRange.Text = 'NEW_TEXT';
I usually create a unique string in the template with a unique string, like %%UNIQUE_STRING%% and I search for this string to replace it using the method I explained.
You can also create the heading by script:
wordRange.Text = 'HEADING_TEXT';
wordRange.Style = -2; % wdStyleHeading1
I hope these two approaches can already give you some directions.
Best regards,
  1 个评论
Alessandro
Alessandro 2021-5-11
Hi Thomas,
thanks for the answer. Your solution would work if the content would be only text.
Yesterday I came with the follow solution:
actx_word.Selection.GoTo(11, 1, Nr);%<-Go to heading
actx_word.Selection.GoTo(3, 2, 1);%<-Go 1 Line down
Start = actx_word_p.Application.Selection.Start; % Save current position
actx_word.Selection.GoTo(11, 1, Nr+1);%<-Go to the next heading
if (actx_word.Application.Selection.end < Start)%there was no next heading. So put end position to end of document
actx_word.Application.Selection.end = actx_word.activedocument.content.end;
end
if Start<actx_word_p.Application.Selection.end %only use Delete if Start is smaller as end or it will delete a character
actx_word.Application.Selection.Start = Start;
actx_word.Application.Selection.Delete
end
It did work well. It doesnt feel right but looks like it would work :). And you are right word connection is not an easy task !
Best regards,
Alessandro

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by