.NET
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Check if file open
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I need to export data to an excel file but I need to check if the file is already open.
Searching the internet I found this but I don't know what "imports" to use to use to enable it.
Public Function FileInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
FileClose(F)
Catch
Return True
End Try
End If
End Function
Solved! Go to Solution.
Re: Check if file open
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Looks like you need to import Microsoft.VisualBasic to get the FileOpen and FileClose functions. You may want to change them to use IO.File.Open and IO.File.Close though, to get away from the old VB syntax.
Brent McAnney
Re: Check if file open
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
I don't know what is the namespaces you use
in your program to work with Excel
Anyway you may want to try this function as well
'Imports Microsoft.Office.Interop.Excel
'Imports Excel = Microsoft.Office.Interop.Excel
Public Function IsExcelFileOpen(xlapp As Microsoft.Office.Interop.Excel.Application, filename As String) As Boolean
Dim isopen As Boolean = True
Dim wbook As Microsoft.Office.Interop.Excel.Workbook
Try
wbook = xlapp.Workbooks(filename)
Catch
isopen = False
End Try
Return isopen
End Function
~'J'~
C6309D9E0751D165D0934D0621DFF27919
Re: Check if file open
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Hi,
I added Imports Microsoft.VisualBasic but it didn't support the problem
OpenMode.Binary is the only one it has a problem with it seems.
Re: Check if file open
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
Microsoft.VisualBasic.OpenMode.Binary

“We don’t have a snowball’s chance in a blast furnace of surviving this attack unless every one of our units gets into the fight right now!”
