How to stop actxserver MS word from having pop ups?

8 次查看(过去 30 天)
So my code's purpose is to search a folder with many files, subfolders, and sub-subfolders, and copy the word documents with a key phrase to a destination folder somewhere else. I tried it a couple times on test folders and it worked. Then I tried it on the actual folder but it ran into some pop ups like the one below.
I wanted to know if there was a way to bypass pop ups like this one:
I found another answered question that soved a similar problem by changing this line:
%from this
Docs = Word.Documents.Open(tempfilepath);
%to this
Docs = Word.Documents.Open(tempfilepath,[],1);
this is the answer
I also wanted to ask if there was a way to speed this code up, I am not a good coder but i know that there are many ways of doing the same process, ones more efficient than the others. Is there another way i could look in the word file?
Here is my code:
path = 'Some directory';
filesfolders = dir(fullfile(path,'**\*.*')); %get list of files and folders in any subfolder
filesfolders = filesfolders(~ismember({filesfolders.name},{'.' '..'}));% removes the . and .. folders from the struct
allfiles = filesfolders(~[filesfolders.isdir]);%removes all the folders and leaves the files
names = {allfiles.name};
c=struct2cell(allfiles);
for i = 1:length(names)
filepath = append(c(2,i),'\',c(1,i)); %returns 1x1 cell of the ith filepath
try % i used try because some word files were corrupted and were giving me errors
if contains(filepath,'.docx') == 1 % checks if its a word doc
if wordlooker(filepath{1})==1
copyfile(filepath{1},"Destination folder"); %copies the word file to the destination folder
end
end
catch
continue
end
end
function bool = wordlooker(tempfilepath) %returns 1 if it finds the phrase in the word doc
Word = actxserver('Word.application'); % Connect to Word
Word.Visible = 0;
Docs = Word.Documents.Open(tempfilepath,[],1); % Open the Document
selection = Word.Selection;
bool = selection.Find.Execute('Key phrase'); % Find the Keyword
Docs.Close;
Word.Quit;
%invoke(Word,'Quit'); % Close word
end
I know this is a very long question but i would be very grateful for any help =)

回答(1 个)

Chetan
Chetan 2023-11-6
编辑:Chetan 2023-11-6
According to my understanding, you are encountering issues with the "actxserver" server when dealing with pop-ups.
You can resolve this by adjusting the "DisplayAlerts" property of the Word application object to false. This action will effectively suppress all Word dialog boxes that require user interaction.
To enhance the efficiency of the code, I would suggest opening the Word application just once, using it for each file, and then closing it once all files have been processed. You can achieve this by making some modifications to the "wordlooker" function.
Here's the revised code that incorporates these changes:
path = 'Some directory';
filesfolders = dir(fullfile(path,'**\*.*')); %get list of files and folders in any subfolder
filesfolders = filesfolders(~ismember({filesfolders.name},{'.', '..'}));% removes the . and .. folders from the struct
allfiles = filesfolders(~[filesfolders.isdir]);%removes all the folders and leaves the files
names = {allfiles.name};
c = struct2cell(allfiles);
% Connect to Word
Word = actxserver('Word.application');
Word.DisplayAlerts = false; % Suppress dialog boxes
Word.Visible = 0;
for i = 1:length(names)
filepath = append(c(2,i),'\',c(1,i)); %returns 1x1 cell of the ith filepath
try % i used try because some word files were corrupted and were giving me errors
if contains(filepath,'.docx') == 1 % checks if its a word doc
if wordlooker(Word, filepath{1}) == 1
copyfile(filepath{1},"Destination folder"); %copies the word file to the destination folder
end
end
catch
continue
end
end
% Close word
Word.Quit;
function bool = wordlooker(Word, tempfilepath) %returns 1 if it finds the phrase in the word doc
Docs = Word.Documents.Open(tempfilepath,[],1); % Open the Document
selection = Word.Selection;
bool = selection.Find.Execute('Key phrase'); % Find the Keyword
Docs.Close;
end
You can refer to the following MathWorks Documentation :
Hope it Helps !

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by