Vba (ဥပမာနှင့်အတူ) text file ကိုမည်သို့ဖွင့်နည်း
သီးခြားဖိုင်လမ်းကြောင်းတစ်ခုမှ စာသားဖိုင်တစ်ခုကိုဖွင့်ရန် VBA ရှိ OpenTextFile နည်းလမ်းကို သင်အသုံးပြုနိုင်သည်။
ဤနည်းလမ်းကို လက်တွေ့တွင် အသုံးပြုရန် ဘုံနည်းလမ်းတစ်ခုဖြစ်သည်။
SubReadTextFile ()
Dim FSO As New FileSystemObject
Set FSO = CreateObject(" Scripting.FileSystemObject ")
'specify path to text file
Set MyTextFile = FSO.OpenTextFile(" C:\Users\bob\Desktop\MyTextFile.txt ", ForReading)
'open text file and display contents in cell A1
TxtString = MyTextFile.ReadAll
MyTextFile.Close
ThisWorkbook.Sheets(1).Range(" A1 ").Value = TxtString
End Sub
ဤ Macro သည် ကျွန်ုပ်၏ကွန်ပျူတာ၏ ဒက်စ်တော့တွင်ရှိသော MyTextFile.txt ဟုခေါ်သော စာသားဖိုင်ကို ဖတ်ပြီး ဆဲလ် A1 တွင် ဖိုင်၏အကြောင်းအရာများကို ပြသသည်။
အောက်ဖော်ပြပါ ဥပမာသည် စာသားဖိုင်ကို လက်တွေ့တွင်ဖတ်ရန် ဤ syntax ကိုအသုံးပြုပုံကို ပြသထားသည်။
ဥပမာ- VBA သုံးပြီး စာသားဖိုင်ကို ဘယ်လိုဖွင့်မလဲ။
ကျွန်ုပ်တို့တွင် VBA ကိုအသုံးပြု၍ Excel တွင်ဖတ်လိုသော desktop ပေါ်တွင် MyTextFile.txt ဟုခေါ်သော စာသားဖိုင်တစ်ခုရှိသည်ဆိုကြပါစို့။
ဤသည်မှာ ဖိုင်၏ အကြောင်းအရာဖြစ်သည်-
ဤဖိုင်ကိုဖတ်ရန် VBA ကို အသုံးမပြုမီ၊ VB တည်းဖြတ်မှုတွင် Microsoft Scripting Runtime ကို ဦးစွာဖွင့်ရပါမည်။
ဒါကိုလုပ်ဖို့၊ VB editor ကိုဖွင့်ပြီး Tools ကိုနှိပ်ပြီး References ကို နှိပ်ပါ။
ပေါ်လာသည့်ဝင်းဒိုးအသစ်တွင်၊ Microsoft Scripting Runtime ကို တွေ့သည်အထိ အောက်သို့ဆင်းကာ ၎င်းဘေးရှိ ဘောက်စ်ကို အမှန်ခြစ်ပေးပါ။ ထို့နောက် OK ကိုနှိပ်ပါ။
ထို့နောက် စာသားဖိုင်ကိုဖတ်ရန် အောက်ပါ macro ကို ဖန်တီးနိုင်သည် ။
SubReadTextFile ()
Dim FSO As New FileSystemObject
Set FSO = CreateObject(" Scripting.FileSystemObject ")
'specify path to text file
Set MyTextFile = FSO.OpenTextFile(" C:\Users\bob\Desktop\MyTextFile.txt ", ForReading)
'open text file and display contents in cell A1
TxtString = MyTextFile.ReadAll
MyTextFile.Close
ThisWorkbook.Sheets(1).Range(" A1 ").Value = TxtString
End Sub
ဤမက်ခရိုကို လုပ်ဆောင်ပြီးသည်နှင့်၊ MyTextFile.txt ဟုခေါ်သော စာသားဖိုင်၏ အကြောင်းအရာများကို ဆဲလ် A1 တွင် ပြသပါမည်။
ဆဲလ် A1 ၏ အကြောင်းအရာများသည် စာသားဖိုင်၏ အကြောင်းအရာများနှင့် ကိုက်ညီကြောင်း သတိပြုပါ။
မှတ်ချက် – OpenTextFile နည်းလမ်းအတွက် စာရွက်စာတမ်းအပြည့်အစုံကို ဤနေရာတွင် ရှာနိုင်သည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် VBA တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-
VBA ကို အသုံးပြု၍ ဖိုင်တွဲများဖန်တီးနည်း
VBA သုံးပြီး ဖိုင်တွဲတွေကို ဘယ်လိုဖျက်မလဲ။
VBA သုံးပြီး ဖိုင်တွေကို ဘယ်လိုဖျက်မလဲ။