[Commonly used User-Defined Function 001] VBA opens files with High Fault Tolerance

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.