2013-05-03 8 views
23

मेरे पास यह कोड है। यह जांचना है कि कोई फ़ाइल मौजूद है या नहीं और इसे खोलने पर इसे खोलें। यह फ़ाइल काम करता है अगर यह काम करता है, और यदि ऐसा नहीं होता है, हालांकि, जब भी मैं टेक्स्टबॉक्स को खाली छोड़ देता हूं और सबमिट बटन पर क्लिक करता हूं, तो यह विफल हो जाता है। मैं क्या चाहता हूं, यदि टेक्स्टबॉक्स खाली है तो त्रुटि संदेश प्रदर्शित करना है जैसे फ़ाइल मौजूद नहीं है।वीबीए जांचें कि फ़ाइल मौजूद है

रनटाइम त्रुटि "1004"

Dim File As String 
File = TextBox1.Value 
Dim DirFile As String 

DirFile = "C:\Documents and Settings\Administrator\Desktop\" & File 
If Dir(DirFile) = "" Then 
    MsgBox "File does not exist" 
Else 
    Workbooks.Open Filename:=DirFile 
End If 
+0

आपने कोड का समस्या हिस्सा प्रदान नहीं किया है (यानी फॉर्म जिसमें सबमिट बटन है)। क्या आप अपनी फाइल साझा कर सकते हैं? – brettdj

+0

उपरोक्त कोड मेरे सबमिट बटन की सामग्री है –

उत्तर

42

कुछ इस

सबसे अच्छा की तरह (यदि आवश्यक) खोला कार्यपुस्तिका भी अधिक नियंत्रण प्रदान करने के लिए

कि फ़ाइल नाम का परीक्षण करने के लिए अद्यतन एक वास्तविक कार्यपुस्तिका था - जो भी प्रारंभिक बनाता है जांच से बेमानी, अन्य संदेश में से पाठ बॉक्स से उपयोगकर्ता फ़ाइल होने की जाँच करने के लिए खाली है

Dim strFile As String 
Dim WB As Workbook 
strFile = Trim(TextBox1.Value) 
Dim DirFile As String 
If Len(strFile) = 0 Then Exit Sub 

DirFile = "C:\Documents and Settings\Administrator\Desktop\" & strFile 
If Len(Dir(DirFile)) = 0 Then 
    MsgBox "File does not exist" 
Else 
On Error Resume Next 
Set WB = Workbooks.Open(DirFile) 
On Error GoTo 0 
If WB Is Nothing Then MsgBox DirFile & " is invalid", vbCritical 
End If 
+1

यह विधि 100% विश्वसनीय नहीं है क्योंकि यह किसी फ़ोल्डर नाम से फ़ाइल नाम को अलग नहीं करती है। –

+1

@iDevlop मैं फ़ाइल नाम का परीक्षण करने के लिए एक कदम चला गया है मान्य है। – brettdj

0

शायद यह फ़ाइल नाम की वजह से चर

File = TextBox1.Value 

यह होना चाहिए

Filename = TextBox1.Value 
+3

यह एक बुरा जवाब नहीं है। "फ़ाइल" या किसी अन्य कीवर्ड का उपयोग करके variablename के रूप में बहुत से लोगों के लिए परेशानी हुई है। हालांकि यह समस्या का समाधान नहीं है, फिर भी यह एक अच्छा मुद्दा है। – AnyOneElse

-5

यो आपको TextBox1 मान को जांचने के लिए एक शर्त लूप सेट करना चाहिए।

If TextBox1.value = "" then 
    MsgBox "The file not exist" 
    Exit sub 'exit the macro 
End If 

आशा है कि यह आपकी मदद करे। किसी कार्यपुस्तिका चर का उपयोग करने के लिए

22

इस समारोह का उपयोग करें:

Function IsFile(ByVal fName As String) As Boolean 
'Returns TRUE if the provided name points to an existing file. 
'Returns FALSE if not existing, or if it's a folder 
    On Error Resume Next 
    IsFile = ((GetAttr(fName) And vbDirectory) <> vbDirectory) 
End Function 
+1

चूंकि आपके पास 'मुख्य त्रुटि फिर से शुरू करें' है, आपकी मुख्य पंक्ति के बाद मैं त्रुटि को रोकने से रोकने के लिए 'ऑन त्रुटि GoTo 0'' पेश करूंगा। वैसे भी, मुझे इस दृष्टिकोण को पसंद है क्योंकि कोई फ़ाइल के अस्तित्व को गलती से किसी फ़ोल्डर के अस्तित्व की जांच के बिना जांच सकता है। – ZygD

+1

क्या यह उस मामले को संभालता है जहां fName न तो फ़ाइल है और न ही निर्देशिका है? ऐसा लगता है कि @brettdj और iDevlop के उत्तरों का संयोजन सबसे अच्छा होगा: _IsFile = ((GetAttr (fName) और vbDirectory) <> vbDirectory) और लेन (Dir (Dirfile)) <> 0_ – riderBill

+2

आगे की जांच, ऐसा लगता है कि _GetAttr (fName) _ अपवाद बढ़ाएगा 53 - FileNotFoundException, फिर से शुरू करने का आह्वान, और IsFile अपना पूर्व मान (गलत) रखेगा। तो आपका काम _does_ सभी मामलों को संभालता है। मैं शायद इसका परीक्षण नहीं करूंगा, लेकिन यह ब्रेट्डज की तुलना में तेज़ी से भी चला सकता है क्योंकि यह डिर का आह्वान नहीं करता है, जो सिस्टम कमांड (?) की तरह संदिग्ध रूप से दिखता है। मेरे सी/सी ++ अनुभव से, सिस्टम कमांड का आह्वान करने में लगभग 1 सेकंड लगते हैं, और निष्पादन योग्य फिर से शुरू करने के लिए शायद दूसरा दूसरा। बढ़िया! मैंने पहले आपके उत्तर को वोट दिया था। मुझे नहीं लगता कि यह शीर्ष वोट गेटर क्यों नहीं है। – riderBill

12

अस्तित्व की जाँच एक भी उपयोग कर सकते हैं (दोनों, फ़ाइलों और फ़ोल्डरों के लिए काम करता है):

Not Dir(DirFile, vbDirectory) = vbNullString 

परिणाम True है अगर एक फ़ाइल या एक निर्देशिका मौजूद है।

उदाहरण:

If Not Dir("C:\Temp\test.xlsx", vbDirectory) = vbNullString Then 
    MsgBox "exists" 
Else 
    MsgBox "does not exist" 
End If 
0

मैं इस वहाँ और फिर बतख बाहर फेंक देंगे। यह जांचने का सामान्य कारण है कि कोई फ़ाइल मौजूद है या नहीं, इसे खोलने का प्रयास करते समय त्रुटि से बचने के लिए। उस से निपटने के लिए त्रुटि हैंडलर का उपयोग करने के बारे में:

Function openFileTest(filePathName As String, ByRef wkBook As Workbook, _ 
         errorHandlingMethod As Long) As Boolean 
'Returns True if filePathName is successfully opened, 
'  False otherwise. 
    Dim errorNum As Long 

'*************************************************************************** 
' Open the file or determine that it doesn't exist. 
    On Error Resume Next: 
    Set wkBook = Workbooks.Open(fileName:=filePathName) 
    If Err.Number <> 0 Then 
     errorNum = Err.Number 
     'Error while attempting to open the file. Maybe it doesn't exist? 
     If Err.Number = 1004 Then 
'*************************************************************************** 
     'File doesn't exist. 
     'Better clear the error and point to the error handler before moving on. 
     Err.Clear 
     On Error GoTo OPENFILETEST_FAIL: 
     '[Clever code here to cope with non-existant file] 
     '... 
     'If the problem could not be resolved, invoke the error handler. 
     Err.Raise errorNum 
     Else 
     'No idea what the error is, but it's not due to a non-existant file 
     'Invoke the error handler. 
     Err.Clear 
     On Error GoTo OPENFILETEST_FAIL: 
     Err.Raise errorNum 
     End If 
    End If 

    'Either the file was successfully opened or the problem was resolved. 
    openFileTest = True 
    Exit Function 

OPENFILETEST_FAIL: 
    errorNum = Err.Number 
    'Presumabley the problem is not a non-existant file, so it's 
    'some other error. Not sure what this would be, so... 
    If errorHandlingMethod < 2 Then 
     'The easy out is to clear the error, reset to the default error handler, 
     'and raise the error number again. 
     'This will immediately cause the code to terminate with VBA's standard 
     'run time error Message box: 
     errorNum = Err.Number 
     Err.Clear 
     On Error GoTo 0 
     Err.Raise errorNum 
     Exit Function 

    ElseIf errorHandlingMethod = 2 Then 
     'Easier debugging, generate a more informative message box, then terminate: 
     MsgBox "" _ 
      & "Error while opening workbook." _ 
      & "PathName: " & filePathName & vbCrLf _ 
      & "Error " & errorNum & ": " & Err.Description & vbCrLf _ 
      , vbExclamation _ 
      , "Failure in function OpenFile(), IO Module" 
     End 

    Else 
     'The calling function is ok with a false result. That is the point 
     'of returning a boolean, after all. 
     openFileTest = False 
     Exit Function 
    End If 

End Function 'openFileTest() 
संबंधित मुद्दे