How can I have MATLAB find and replace text in a MS Word document

77 ビュー (過去 30 日間)
Robert
Robert 2012 年 2 月 8 日
コメント済み: Merve 2023 年 3 月 3 日
I am running MATLAB to read in several txt documents and crunch the numbers and provide a formated MS Word document as an output.
I have a template Word document that has 8 lines that need to be modified based on the number crunching MATLAB does for me. I can have MATLAB make a string variable that says the words that need to be placed in the Word document at the specific points.
The problem I have is how to have MATLAB open the Word document and find/replace the desired lines of text.
Are there any ideas?
Thanks!

回答 (3 件)

Astik Sachan
Astik Sachan 2017 年 3 月 22 日
編集済み: Astik Sachan 2017 年 8 月 10 日
You can use following code as well and edit it as per need!
[fname path] = uigetfile('*.doc*');
fpath = [path fname];
find_txt = 'Text';
replace_txt = 'Replaced';
w = actxserver('Word.Application');
w.Documents.Open(fpath);
w.Selection.Find.Font.Size = 20.5;
w.Selection.Find.Font.Name = 'Calibri';
w.Selection.Find.Execute(find_txt,1,0,0,0,0,1,0,1,replace_txt,2,0,0,0,0); %forward
w.Selection.Find.Execute(find_txt,1,0,0,0,0,0,0,1,replace_txt,2,0,0,0,0); %backward
w.ActiveDocument.Save
w.ActiveDocument.Close
For More Information about other arguments used in Find Execute function you can visit: https://msdn.microsoft.com/en-us/library/office/ff193977.aspx
  2 件のコメント
Arnaud Bitoun
Arnaud Bitoun 2019 年 10 月 12 日
Thank you Astik for this helpful code. How do you do if you are looking to change text in the Header ?
Merve
Merve 2023 年 3 月 3 日
thank you for the code, it is very helpful but it doesn't change the text in header.

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


Robert
Robert 2012 年 2 月 10 日
I was able to figure it out after piecing together lots of stuff I found on the internet. Basically, to find all the "hello" text and replace them with "goodbye" in the document D:\Doc1.doc, you:
Word = actxserver('Word.application'); Wrod.Visible = 0; set(Word,'DisplayAlerts',0); Docs = Word.Documents; Doc = Docs.Open('D:\Doc1.doc'); setection = Word.Selection; selection.Find.Execute('<text you want to find>',0,0,0,0,0,1,1,0,'<Text you want to replace>',2,0,0,0,0); Doc.Save; Docs.Close; invoke(Word,'Quit'); delete(Word);
All the zeros and ones correspond to different settings of the Execute method. Most of the zeros correspond to formatting and the ones correspond to continueing the find. The two corresponds to replace all. These can be found if you look up the Execute method.
Hope this helps someone else.
  1 件のコメント
Robert
Robert 2012 年 2 月 10 日
<text you want to find> = hello
<text you want to replace> = goodbye

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


Gordon
Gordon 2015 年 3 月 19 日
編集済み: Walter Roberson 2019 年 10 月 12 日
Tried this code and it works. Corrected one typo though. Code should be:
Word = actxserver('Word.application');
Wrod.Visible = 0;
set(Word,'DisplayAlerts',0);
Docs = Word.Documents;
Doc = Docs.Open('D:\Doc1.doc');
selection = Word.Selection; selection.Find.Execute('<text you want to find>',0,0,0,0,0,1,1,0,'<Text you want to replace>',2,0,0,0,0);
Doc.Save;
Docs.Close;
invoke(Word,'Quit');
delete(Word);
  1 件のコメント
Gordon
Gordon 2015 年 3 月 19 日
編集済み: Walter Roberson 2019 年 10 月 12 日
Corrected another typo. Should be:
Word = actxserver('Word.application');
Word.Visible = 0;
set(Word,'DisplayAlerts',0);
Docs = Word.Documents;
Doc = Docs.Open('D:\Doc1.doc');
selection = Word.Selection;
selection.Find.Execute('<text you want to find>',0,0,0,0,0,1,1,0,'<Text you want to replace>',2,0,0,0,0);
Doc.Save;
Docs.Close;
invoke(Word,'Quit');
delete(Word);

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

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by