اذهب الي المحتوي
أوفيسنا
بحث مخصص من جوجل فى أوفيسنا
Custom Search

خطأ في كود Catch Try


ناصراا
إذهب إلى أفضل إجابة Solved by ابو تراب,

الردود الموصى بها

السلام عليكم 

 

 

اساتذتي الكرام 

 

اعتذر منكم فقد كثرت طلباتي 

 

 

اين الخطأ في هذا الكود تكرما 

Dim st As Integer
    Try
  st = GetFileContents("C:\111.txt")
                Catch
MsgBox "تنبيه ! لا يوجد ملف "
                EndTry

 

 
 
 
 
 
 
 
 
رابط هذا التعليق
شارك

  • أفضل إجابة

اخي الفاضل هذا مثال يقرأ من ملف نصي الى الاكسل.

 

يمكنك بدلا من استخدام Try/Catch ان تستخدم On Error كما في الكود التالي:

 

لتطبيق المثال حمل المرفق و تأكد ان ملف النص موجود في المسار D:\myData\prices.txt

 

Sub btnReadTextFile()


Dim myFile As String, text As String, textline As String
Dim item As Integer, price As Integer, qty As Integer


myFile = "D:\myData\prices.txt"


On Error GoTo ErrHandler:


Open myFile For Input As #1


Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
Loop


Close #1


item = InStr(text, "المادة")
price = InStr(text, "السعر")
qty = InStr(text, "الكمية")


Range("B3").Value = Trim(Mid(text, item + 8, 15))
Range("B4").Value = Mid(text, price + 7, 5)
Range("B5").Value = Mid(text, qty + 8, 5)


Exit Sub


ErrHandler:


MsgBox "لم يتم العثور على الملف ", vbCritical + vbOKOnly, "فتح ملف"


End Sub

 

MyData.zip

  • Like 1
رابط هذا التعليق
شارك

شكرا لك استاذي ابو تراب 

 

ممتاز جدا 

 

وقام on error بنفس الدور 

 

 

استخدمت الكود كالتالي 

ليجلب أول سطر فقط  فلا احتاج المزيد 

 

 

 

لكن سؤالي تكرما 

 

ما معنى جملة  (Input As #1 ) فلم أفهمها 

Sub btnReadTextFile()


Dim myFile As String, text As String, textline As String



myFile = "d:\myData\prices.txt"

On Error GoTo ErrHandler:

Open myFile For Input As #1



    Line Input #1, textline
    text = textline



Close #1

Range("B3").Value = textline

Exit Sub
ErrHandler:
MsgBox "لم يتم العثور على الملف ", vbCritical + vbOKOnly, "فتح ملف"
End Sub

رابط هذا التعليق
شارك

حياك الله ناصر

 

معنى جملة Input As #1 هو فتح الملف للقرأة و 1# هو رقم مرجعي للملف (prices.txt) حيث استخدمنا هذا الرقم لقرأة سطر من الملف كما في الجملة Line Input #1, textline و اغلقنا الملف كما في الجملة Close #1

  • Like 1
رابط هذا التعليق
شارك

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

زائر
اضف رد علي هذا الموضوع....

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • تصفح هذا الموضوع مؤخراً   0 اعضاء متواجدين الان

    • لايوجد اعضاء مسجلون يتصفحون هذه الصفحه
×
×
  • اضف...

Important Information