フィルターのクリア

How to use activex to search Word documents for a phrase and delete the document?

15 ビュー (過去 30 日間)
Calabrese
Calabrese 2017 年 6 月 9 日
編集済み: Matt 2020 年 3 月 5 日
I would like to search through ~100 Microsoft Word documents for a specific phrase (survey), if the document contains (survey) anywhere within the document, I would like to delete the document.
Code at this link works to create a word document, however I am trying to pick it apart to search an existing document.
Then I would need a 'for loop' to cycle through the ~100 documents and delete the document if (survey) is found with an 'if statement'.

採用された回答

Guillaume
Guillaume 2017 年 6 月 15 日
When all you have is a hammer everything looks like a nail!
Rather than trying to work out how to automate word from matlab, why not simply use the search feature of windows file explorer. Put survey in your search box and restrict the search to word documents. When the result are returned, select all, press delete. done.
  1 件のコメント
Matt
Matt 2020 年 3 月 5 日
編集済み: Matt 2020 年 3 月 5 日
I know I'm very late to the party, but sometimes, there are pedagogical reasons to use a hammer . I'm not saying that your idea is bad at all (and it is where I would have started as well) but there is a learning feature to banging your head as we all know...

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

その他の回答 (1 件)

Nirav Sharda
Nirav Sharda 2017 年 6 月 12 日
You can use the 'Word.Selection.Find.Execute' function to find a keyword and then use the delete function in MATLAB to delete files. I am creating a small example for your reference.
% Connect to Word
Word = actxserver('Word.application');
Word.Visible = 0;
Docs = Word.Documents;
% Open the Document
Doc = Docs.Open('filename');
% Find the Keyword
selection = Word.Selection;
logicalOutput = selection.Find.Execute('keyword')
% This will give the document name, you can save it to a cell array
Doc.FullName
% Close word
invoke(Word,'Quit');
delete(Word);
Once this is done you can delete all the files that match the criteria. I hope this helps.
  2 件のコメント
Calabrese
Calabrese 2017 年 6 月 13 日
編集済み: Calabrese 2017 年 6 月 13 日
I get the following errors, see attached, I tried multiple different inputs.
Nirav Sharda
Nirav Sharda 2017 年 6 月 15 日
You will have to provide the complete path and then the filename. Here is the Microsoft documentation for that.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by