dbTalk Databases Forums  

Re: Check XLS file status in DTS

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Re: Check XLS file status in DTS in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Matrix
 
Posts: n/a

Default RE: Check XLS file status in DTS - 09-22-2004 , 12:41 PM






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:

Quote:
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.

Reply With Quote
  #2  
Old   
Allan Mitchell
 
Posts: n/a

Default Re: Check XLS file status in DTS - 09-22-2004 , 04:40 PM






In article <9A0B2B3A-3F77-4401-91CD-5C14B4E0BFE3 (AT) microsoft (DOT) com>, Matrix wrote:
Quote:
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





Reply With Quote
  #3  
Old   
Matrix
 
Posts: n/a

Default Re: Check XLS file status in DTS - 09-23-2004 , 11:43 AM



I got it working. The way its working is I got one excel file to hold the
macro in VBA code that checks the source file is in use or not.
Function Main()
Dim filename
Dim filenum
Dim strResult


filename = "c:\Myxls.xls"

Set ExcelApp = CreateObject("excel.application")
ExcelApp.Visible = False

ExcelApp.Workbooks.Open ("c:\Book1.xls")
strResult = ExcelApp.Run("IsFileOpen")
ExcelApp.Quit
Set ExcelApp = Nothing

IF strResult = True then
Main = DTSTaskExecResult_Failure
ELSE
Main = DTSTaskExecResult_Success
END IF

End Function

Tested and working as expected.

"Allan Mitchell" wrote:

Quote:
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





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.