Note that there are some explanatory texts on larger screens.

plurals
  1. POWord vba document.readonly status incorrectly returns false
    text
    copied!<p>I have an excel project that checks word documents for a changed modify date and, if changed, it opens that document and imports the text from the word form fields into excel.</p> <p>The routine in excel that opens and imports the word documents is as follows:</p> <pre><code>Sub CopyFromWord(pFile as String,aFile as string) Dim wdApp As Object On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err.Number &lt;&gt; 0 Then Set wdApp = CreateObject("Word.Application") Else 'word is already running End If On Error Goto 0 Set wdDoc = wdApp.Documents.Open(Filename:="" &amp; pFile &amp; "", ReadOnly:=True) wdApp.Visible = False For Each c In wdDoc.bookmarks 'removed code that copies values from word doc fields to excel sheet Next c wdApp.Activedocument.Close SaveChanges:=False End Sub </code></pre> <p>The word documents all started out as copies of the same file. There are a few thousand copies of the file, but each located in their own folder with a unique name.</p> <p>The user finds the folder they need and opens the word document within it. It bring up a userform and then populates formfields in the document with the input to the userform. A command button then saves and exits the form.</p> <p>Because the welcome message/userform loads automatically upon the document opening, I added the following code into the open event for the document:</p> <pre><code>Sub Document_Open() If ThisDocument.ReadOnly = True then Exit Sub msgbox "Welcome " &amp; Environ$("Username") &amp; ". Click OK to begin." Userform1.show End sub </code></pre> <p>This ensures when the excel project loops through all the files, if it finds one has changed, it needs to open the file (read only) so it can import the data without being interrupted with a userform / welcome message, close it, and carry on searching looping all files checking for changed modify-dates.</p> <p>It should run constantly, however, about 20% of the time, a document will be opened read only by the excel code, but the welcome messagebox in the word document will show, indicating thisdocument.readonly incorrectly returned false. If I debug the word document in this scenario, and do</p> <pre><code>? thisdocument.readonly </code></pre> <p>I get a "false" result. However, even the title bar of the word document ends with " (Read-Only)" so it has clearly been opened read-only, thus readonly should return True.</p> <p>It is not specific to any documents, if I try to repeat opening them it seems to work the next time round (in that it correctly registers a read-only and exits the sub before the messagebox code). I cant find any kind of pattern and can't find any info online, I've been searching this for weeks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload