1
votes

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.

The routine in excel that opens and imports the word documents is as follows:

Sub CopyFromWord(pFile as String,aFile as string)

Dim wdApp As Object

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
    Set wdApp = CreateObject("Word.Application")
Else 'word is already running
End If
On Error Goto 0

Set wdDoc = wdApp.Documents.Open(Filename:="" & pFile & "", 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

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.

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.

Because the welcome message/userform loads automatically upon the document opening, I added the following code into the open event for the document:

Sub Document_Open()

If ThisDocument.ReadOnly = True then Exit Sub

msgbox "Welcome " & Environ$("Username") & ". Click OK to begin."
Userform1.show

End sub

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.

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

? thisdocument.readonly

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.

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!

1
I don't know the answer to your specific problem, but one work-around would be to disable macros before you open the document from Excel: social.msdn.microsoft.com/Forums/fi-FI/vbgeneral/thread/…Tim Williams
My hunch is you are somehow not correctly closing the documents some of the time and so periodically the word document is not being opened correctly. The fact that you didn't mention the userform being shown is also interesting to me. Last comment would be to try to remove the On Error stuff or add some sort of debug info to the Else part and see if somehow the times ReadOnly fails relates to errors there.enderland
how do I correctly close the document then? The userform displays once the OK is pressed to the message box. If I close the userform, the code then carries on, grabbing the contents of the word doc and then closing it. So I just have to keep checking it periodically and clicking OK to let it carry on when it gets stuck.pedromillers
Thanks Tim Williams for the lead on disabling macros, however it doesn't seem to work, the message box will still come up. It only seems to affect if I open the word docs manually, when opened via the excel code it seems to automatically enable the macros again.pedromillers

1 Answers

1
votes

May not be considered the answer, but following Tim William's suggestion, I managed to put together this which completely solves my problem. I struggled at first because I was trying to set the property too early. Complete code is as follows:

 Sub CopyFromWord(pFile as String)

 Dim wdApp As Object

 On Error Resume Next
 Set wdApp = GetObject(, "Word.Application")
 If Err.Number <> 0 Then
 Set wdApp = CreateObject("Word.Application")
 Else 'word is already running
 End If
 On Error Goto 0

'save current setting
secAutomation = wrdApp.Application.AutomationSecurity

'set Word to disable macros when a document is opened via vb:
wrdApp.Application.AutomationSecurity = msoAutomationSecurityForceDisable
'(without using wrdApp prefix it would only apply to the code's App i.e. Excel)

Set wdDoc = wdApp.Documents.Open(Filename:="" & pFile & "", 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

'restore original setting before closing
wrdApp.Application.AutomationSecurity = secAutomation

wdApp.Activedocument.Close SaveChanges:=False

End Sub

Many thanks to Tim Williams for the link, and the guy who provided the code within the content of that link. This was such a help and is most appreciated.