add this code to a Module:
' https://exceloffthegrid.com/vba-find-file-already-open/
Function IsFileOpen(filename As String)
Dim fileNum As Integer
Dim errNum As Integer
'Allow all errors to happen
On Error Resume Next
fileNum = FreeFile()
'Try to open and close the file for input.
'Errors mean the file is already open
Open filename For Input Lock Read As #fileNum
Close fileNum
'Get the error number
errNum = Err
'Do not allow errors to happen
On Error GoTo 0
'Check the Error Number
Select Case errNum
'errNum = 0 means no errors, therefore file closed
Case 0
IsFileOpen = False
'errNum = 70 means the file is already open
Case 70
IsFileOpen = True
'Something else went wrong
Case Else
IsFileOpen = errNum
End Select
End Function
on your code, you also add an array and put
the path + name of the word file that you are working
on:
Public aFiles(1 To X) As String 'X is the total number of files
aFiles(1)="doc path + filename 1 here"
aFiles(2)="doc path + filename 2 here"
' etc...
***********
now on your code make some modification:
For i = 1 To arrNoofDocs
If IsFileOpen(aFiles(i)) <> 0
Set aDoc = aDocArray(i)
On Error Resume Next
If Not aDoc Is Nothing Then aDoc.Close
'aDoc.Close
End If
Next