baabaa_nl
asked on
vb.net, close a file that is already open
I need the code in vb.net that will check if the file is open and then close the file.
Thanks in advance
Thanks in advance
is it when you have opened the file using your program or when another program has the file open?
ASKER
SiddharthRout--> yes i would like a sample code.
nepaluz--> the file that was opened by another program
Thanks in advance guys
nepaluz--> the file that was opened by another program
Thanks in advance guys
A very basic example :)
Note: If there are multiple instance of Excel then you might have to use TRY/CATCH to loop though all instances and then close all the workbooks.
HTH
Sid
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim xlApp As New Excel.Application
Dim xlWb As Excel.Workbook
xlApp = GetObject(, "Excel.Application")
For Each xlWb In xlApp.Workbooks
xlWb.Close(False)
Next
End Sub
End Class
Note: If there are multiple instance of Excel then you might have to use TRY/CATCH to loop though all instances and then close all the workbooks.
HTH
Sid
ASKER
well, could you please give me a code that can close any opened file which was opened by another application.
Cheers
Cheers
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your code. It was an excellent work.
Sid