Opening word documents that are password protected in "read-only mode": how to close the pop-up window from Matlab

11 views (last 30 days)
Hi everybody,
I have a database of word documents from which I want to do a cross-references of the table of contents of all those documents. the goal here is to be able to know where various information can be found, without having to open all of those documents manually (132 documents).
Using a very preliminary code and a document example, I manage to make it work using a solution found in another question:
the preliminary code I wrote looks like this:
word = actxserver('Word.Application');
title = 'Test1.doc';
wdoc = word.Documents.Open([pwd '\' title]);
txt = wdoc.Content.Text;
wdoc.Close;
word.Quit;
However the problem is that all 132 documents are password protected for editing. meaning that when I call "word.Documents.Open", first a Word pop-up window opens and I have to click on "read-only" before Matlab manage to finish this action.
pop-up.png
Once I manually press the "read-only" button all works perfectly fine and I can continue coding by myself.I get the text content in my variable "txt" and from this I can search its content as I want. what I don't know is how to make Matlab close this automatic pop-up window by itself. I'm not even sure it would be at all possible, as matlab tells me it is "busy" until I manually close the pop-up window. anybody has a clue?
I had to blind the title of the .doc document as it is proprietary information.
If it is not possible, I could still consider coding it all, with the exception that for each file I will have to click on this pop-up window manually (132 times,...) but if I can have a fully automatic code that would be the best.
thanks a lot for your help, and sorry if this has been answered already. I tried searching for this specific answer but didn't find anything relevant so far and I'm a bit short on time.
  1 Comment
Bastien Haemmerli
Bastien Haemmerli on 26 Jan 2021
Hi, I ended up doing things manually for the first round, but I have now to do it one more time and a fully automatic version would be great. I tried to find some more information as to how to use activeX and how to control Word through Matlab. however, I failed to find a good overview of all the possibilities that the actxserver proposes.
give links to the VBA library for "documents" object: https://docs.microsoft.com/en-us/office/vba/api/Word.Document
but I'm not sure I understand fully how to use the library.
Notably, it seems that the only way to actual open a word document is to use the varDoc.Documents.Open('fileName.doc') function. this as I understand it (please correct me if I'm wrong) do the same as if I was manually double clicking on the file to open the document.
As mentioned, as the file is password protected, i get the pop-up window to either insert the password, or to open the file as "read only". once I click manually on "read only" I get to access the document. I can then do varDoc.ReadOnly to get the property of the document telling me that indeed this document is read only.
What I guess I would like Matlab to do is to automatically open the file as "read only" to avoid the pop-up window to appear. and actually it seems that this is not possible as even directly with word I cannot open the document without having the pop-up appearing. and hence what I would like to automatize is simply not possible with password protected documents.
do you have any further ideas? as mentioned, I cannot "take control" of the pop-up from Matlab as basically it is "waiting" as busy for the document to be finished with the varDoc.Documents.Open('fileName.doc') action...that is not happening until the pop-up is closed.
does a "OpenAsReadOnly" function exists? I could not find it in the VBA library.
As mentioned, I will have to click 132 times on "Read Only" for this new round which I would gladly avoid.
Thanks a lot for your help.

Sign in to comment.

Accepted Answer

Jan
Jan on 26 Jan 2021
Edited: Jan on 27 Jan 2021
I cannot try it currently by my own:
wdoc = word.Documents.Open([pwd '\' title], [], true);
% [EDITED] true instead of 'true'
  1 Comment
Bastien Haemmerli
Bastien Haemmerli on 27 Jan 2021
Hello Jan, truly MVP :).
Thanks a lot for your help. this worked perfectly. the only "error" in your solution was to pass the 'true' as a string whereas a bool value true (or 1) was required.
The solution that worked for me is then:
wdoc = word.Documents.Open([pwd '\' title],[],true);
% or
wdoc = word.Documents.Open([pwd '\' title],[],1);
Otherwise the information on the page you proposed was spot on.
thanks to you, I fit inside the right box in terms of amount of time I could spend on the program :)

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!