How to use activex to search Word documents for a phrase and delete the document?
Afficher commentaires plus anciens
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'.
Réponse acceptée
Plus de réponses (1)
Nirav Sharda
le 12 Juin 2017
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 commentaires
Nirav Sharda
le 15 Juin 2017
You will have to provide the complete path and then the filename. Here is the Microsoft documentation for that.
Catégories
En savoir plus sur ActiveX dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!