Distinguished Contributor
Posts: 240
Registered: ‎04-15-2011
Message 1 of 5 (244 Views)
Accepted Solution

Check if file open

244 Views, 4 Replies
03-15-2012 01:36 PM

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
                Dim F As Short = FreeFile()
                FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
                Return True
            End Try
        End If
    End Function



Distinguished Contributor
Posts: 406
Registered: ‎12-09-2003
Message 2 of 5 (242 Views)

Re: Check if file open

03-15-2012 01:42 PM in reply to: SRSDS

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

*Expert Elite*
Posts: 1,567
Registered: ‎10-08-2008
Message 3 of 5 (227 Views)

Re: Check if file open

03-15-2012 02:08 PM in reply to: SRSDS

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
            wbook = xlapp.Workbooks(filename)
            isopen = False
        End Try
        Return isopen
    End Function



Distinguished Contributor
Posts: 240
Registered: ‎04-15-2011
Message 4 of 5 (226 Views)

Re: Check if file open

03-15-2012 02:11 PM in reply to: BMcAnney


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.

*Expert Elite*
Posts: 1,285
Registered: ‎06-21-2004
Message 5 of 5 (224 Views)

Re: Check if file open

03-15-2012 02:22 PM in reply to: SRSDS



(defun botsbuildbots() (botsbuildbots))

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you interested in helping shape the Autodesk Community?
We’re looking at a few different ways to improve the “All Forums” landing page and need your feedback! If interested, please take a few minutes to fill out the following Usability Study. Thank you for your time!

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community