Check If FILE is OPEN or NOT

3.2k Views Asked by At

I am working in MS Words Rough.doc file and I want to check if Ceemea & Latam.doc File is open or not. There should be two possible output.

1. If already opened then Activate

2. If not, then open Ceemea & Latam

Using following code but it returning compile error

If AlreadyOpen("C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\CEEMEA & LATAM.doc") Then

    Windows("CEEMEA & LATAM").Activate

Else

    Documents.Open FileName:="C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\CEEMEA & LATAM.doc"

End If
2

There are 2 best solutions below

2
GSerg On BEST ANSWER
sub ActivateOrOpen()
    on error goto nofile
    Windows("CEEMEA & LATAM").Activate
    exit sub

    nofile:
    Documents.Open FileName:="C:\Documents and Settings\Administrator\Desktop\EMEA CEEMEA\CEEMEA & LATAM.doc"
end sub
1
Solivan On

try this function:

Function AlreadyOpen(filePath As String) As Boolean
Dim result As Boolean
result = False
For Each aDoc In Documents
 apath = aDoc.Path + "\" + aDoc.Name
 If apath = filePath Then result = True
Next aDoc
AlreadyOpen = result
End Function