help_for_excel
Board Regular
- Joined
- Feb 24, 2012
- Messages
- 72
Hi....I have the below code to check whether particular file is open or not. It works fine with excel file or powerpoint file checking but when I used with text file like the line which i have commented it doesn't wrk properly...can any one please tell me how to modify this code so I can use with txt files also
Option Explicit
Sub TestVBA()
Const strFileToOpen As String = "C:\Desktop\file1.pptx"
'Const strFileToOpen As String = "C:\Desktop\country.txt"
If IsFileOpen(strFileToOpen) Then
MsgBox strFileToOpen & " is already Open", vbInformation
Else
MsgBox strFileToOpen & " is not open", vbInformation
End If
End Sub
Function IsFileOpen(strFullPathFileName As String) As Boolean
Dim hdlFile As Long
On Error GoTo FileIsOpen:
hdlFile = FreeFile
Open strFullPathFileName For Random Access Read Write Lock Read Write As hdlFile
IsFileOpen = False
Close hdlFile
Exit Function
FileIsOpen:
IsFileOpen = True
Close hdlFile
End Function
Option Explicit
Sub TestVBA()
Const strFileToOpen As String = "C:\Desktop\file1.pptx"
'Const strFileToOpen As String = "C:\Desktop\country.txt"
If IsFileOpen(strFileToOpen) Then
MsgBox strFileToOpen & " is already Open", vbInformation
Else
MsgBox strFileToOpen & " is not open", vbInformation
End If
End Sub
Function IsFileOpen(strFullPathFileName As String) As Boolean
Dim hdlFile As Long
On Error GoTo FileIsOpen:
hdlFile = FreeFile
Open strFullPathFileName For Random Access Read Write Lock Read Write As hdlFile
IsFileOpen = False
Close hdlFile
Exit Function
FileIsOpen:
IsFileOpen = True
Close hdlFile
End Function