Code:
Function openexcelfile (Spath as string, byval sfilename as string, bdisplay as Boolean, spwd as string) as integer
'Open an Excel file
'Ver 1.05
'Completion time: 2007.12.01
'Design: Monkey King software studio www.okexcel.com.cn
'Parameter description:
'Spath: absolute file path; sfilename: Excel file name; bdisplay: true: error message displayed; spwd: file opening Password
'Return value:-1: The file with the same name has been opened;-2: The file does not exist or the password is incorrect; 0: Open successfully; 1: the file has been opened
Dim bopen as Boolean
Dim sfullname as string
On Error resume next
If instr (lcase (sfilename), ". xls") = 0 then sfilename = sfilename & ". xls"
Sfullname = workbooks (sfilename). fullname
'Check whether an Excel file with the same name has been opened
'The sfullname is not empty
On Error goto 0
Bopen = false
If sfullname <> "" then
If lcase (sfullname) = lcase (Spath & "/" & sfilename) then
Bopen = true
'Determine whether a file with the same name has been opened is the file to be opened this time
Openexcelfile = 1
'The file has been opened
Else
If bdisplay then
Msgbox "close the" & sfilename & "file first! "& CHR (13) &" cannot open files of the same name at the same time. This is an Excel rule! ", Vbokonly + vbexclamation," file opening error"
End if
Bopen = true
Openexcelfile =-1
'The file of the same name cannot be opened at the same time. This is an Excel rule
End if
End if
If not bopen then
On Error goto erropen
Workbooks. Open filename: = Spath & "/" & sfilename, password: = spwd
On Error goto 0
Openexcelfile = 0
'File opened successfully
End if
Exit Function
Erropen:
If bdisplay then msgbox err. Description, vbokonly + vbexclamation, "file opening error"
Openexcelfile =-2
'The file does not exist or the password is incorrect.
On Error goto 0
End Function