![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
How can I identify if an excel file is in use or open. If it is in use or open then it should abort the DTS and send an email reporting it. I am using excel file in DTS to do my job but first I want to check if the File is already in use by other user or process? Please help. |
#2
| |||
| |||
|
|
I got VB source that does the samething but How can I convert that to VBScript. Sub TestFileOpened() ' Test to see if the file is open. If IsFileOpen("c:\Myxls.xls") Then ' Display a message stating the file in use. MsgBox "File already in use!" ' ' Add code here to handle case where file is open by another ' user. ' Else ' Display a message stating the file is not in use. MsgBox "File not in use!" ' Open the file in Microsoft Excel. Workbooks.Open "c:\Myxls.xls" ' ' Add code here to handle case where file is NOT open by another ' user. ' End If End Sub ' This function checks to see if a file is open or not. If the file is ' already open, it returns True. If the file is not open, it returns ' False. Otherwise, a run-time error occurs because there is ' some other problem accessing the file. Function IsFileOpen(filename As String) Dim filenum As Integer, errnum As Integer On Error Resume Next ' Turn error checking off. filenum = FreeFile() ' Get a free file number. ' Attempt to open the file and lock it. Open filename For Input Lock Read As #filenum Close filenum ' Close the file. errnum = Err ' Save the error number that occurred. On Error GoTo 0 ' Turn error checking back on. ' Check to see which error occurred. Select Case errnum ' No error occurred. ' File is NOT already open by another user. Case 0 IsFileOpen = False ' Error number for "Permission Denied." ' File is already opened by another user. Case 70 IsFileOpen = True ' Another error occurred. Case Else Error errnum End Select End Function "Matrix" wrote: How can I identify if an excel file is in use or open. If it is in use or open then it should abort the DTS and send an email reporting it. I am using excel file in DTS to do my job but first I want to check if the File is already in use by other user or process? Please help. |
#3
| |||
| |||
|
|
In article <9A0B2B3A-3F77-4401-91CD-5C14B4E0BFE3 (AT) microsoft (DOT) com>, Matrix wrote: I got VB source that does the samething but How can I convert that to VBScript. Sub TestFileOpened() ' Test to see if the file is open. If IsFileOpen("c:\Myxls.xls") Then ' Display a message stating the file in use. MsgBox "File already in use!" ' ' Add code here to handle case where file is open by another ' user. ' Else ' Display a message stating the file is not in use. MsgBox "File not in use!" ' Open the file in Microsoft Excel. Workbooks.Open "c:\Myxls.xls" ' ' Add code here to handle case where file is NOT open by another ' user. ' End If End Sub ' This function checks to see if a file is open or not. If the file is ' already open, it returns True. If the file is not open, it returns ' False. Otherwise, a run-time error occurs because there is ' some other problem accessing the file. Function IsFileOpen(filename As String) Dim filenum As Integer, errnum As Integer On Error Resume Next ' Turn error checking off. filenum = FreeFile() ' Get a free file number. ' Attempt to open the file and lock it. Open filename For Input Lock Read As #filenum Close filenum ' Close the file. errnum = Err ' Save the error number that occurred. On Error GoTo 0 ' Turn error checking back on. ' Check to see which error occurred. Select Case errnum ' No error occurred. ' File is NOT already open by another user. Case 0 IsFileOpen = False ' Error number for "Permission Denied." ' File is already opened by another user. Case 70 IsFileOpen = True ' Another error occurred. Case Else Error errnum End Select End Function "Matrix" wrote: How can I identify if an excel file is in use or open. If it is in use or open then it should abort the DTS and send an email reporting it. I am using excel file in DTS to do my job but first I want to check if the File is already in use by other user or process? Please help. Have you tried to oconvert this? VBScript does not support things like dim strX as String But besides that (and without testing) I cannot see much that will not work. If you find you cannot open the file then you can disable the import step. This would not of course capture the fact that people may open the file in between you checking and you attempting to do something with it -- Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) www.SQLDTS.com - The site for all your DTS needs. www.konesans.com - Consultancy from the people who know |
![]() |
| Thread Tools | |
| Display Modes | |
| |