VBA open file

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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I think this may be related to how the other applicaiton opens the file. I tried your code and found that using the same txt file that:

Excel 2000<TABLE style="BORDER-BOTTOM: #a6aab6 1px solid; BORDER-LEFT: #a6aab6 1px solid; BACKGROUND-COLOR: #ffffff; BORDER-COLLAPSE: collapse; BORDER-TOP: #a6aab6 1px solid; BORDER-RIGHT: #a6aab6 1px solid" rules=all cellPadding=2><COLGROUP><COL style="BACKGROUND-COLOR: #e0e0f0" width=25><COL><COL></COLGROUP><THEAD><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #e0e0f0; COLOR: #161120"><TH></TH><TH>A</TH><TH>B</TH></TR></THEAD><TBODY><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">5</TD><TD>Notepad</TD><TD>Never gives a file is open</TD></TR><TR><TD style="TEXT-ALIGN: center; COLOR: #161120">6</TD><TD>Word</TD><TD>Works correctly</TD></TR></TBODY></TABLE>
Sheet1

Note: Notepad gave an error on save, when the file was also opened in Word.
 
Upvote 0

Forum statistics

Threads
1,215,982
Messages
6,128,099
Members
449,419
Latest member
mammothzaa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top