英文:
encoding='UTF-8' Open strFile For Input As intFile
问题
以下代码 VBA 无法正确读取波斯字母,需要使用正确的编码;编码为 'UTF-8'
Dim strFile As String
Dim intFile As Integer
Dim strBody As String
strFile = "C:\Plus.csv"
intFile = FreeFile
Open strFile For Input As intFile
strBody = Input(LOF(intFile), intFile)
Close intFile
英文:
the following code vba does not read Persian letters with a proper encoding ; encoding='UTF-8'
Dim strFile As String
Dim intFile As Integer
Dim strBody As String
strFile = "C:\Plus.csv"
intFile = FreeFile
Open strFile For Input As intFile
strBody = Input(LOF(intFile), intFile)
Close intFile
答案1
得分: 0
你可以使用Microsoft ActiveX数据对象库中的Stream对象来读取UTF-8编码文件的内容。
早期绑定
对于早期绑定,你首先需要设置一个引用(Visual Basic >> 工具 >> 引用)到以下库...
Microsoft ActiveX数据对象 x.x库
然后你可以使用关键字New来创建一个Stream对象的实例,并读取文件的内容...
Dim theStream As ADODB.Stream
Dim theContents As String
Set theStream = New ADODB.Stream
With theStream
.Charset = "UTF-8"
.Mode = adModeReadWrite
.Type = adTypeText
.Open
.LoadFromFile "c:\users\domta\desktop\sample.txt"
theContents = .ReadText
.Close
End With
晚期绑定
对于晚期绑定,无需设置引用。但是你需要使用CreateObject方法创建一个Stream对象的实例,并读取文件的内容。
Dim theStream As Object
Dim theContents As String
Set theStream = CreateObject("ADODB.Stream")
With theStream
.Charset = "UTF-8"
.Mode = 3 'adModeReadWrite
.Type = 2 'adTypeText
.Open
.LoadFromFile "c:\users\domta\desktop\sample.txt"
theContents = .ReadText
.Close
End With
英文:
You can use the Stream object from the Microsoft ActiveX Data Objects library the read the contents of a UTF-8 encoded file.
Early Binding
For early binding, you'll first need to set a reference (Visual Basic >> Tools >> References) to the following library...
Microsoft ActiveX Data Objects x.x Library
Then you can use the keyword New to create an instance of the Stream object, and read the contents of the file...
Dim theStream As ADODB.Stream
Dim theContents As String
Set theStream = New ADODB.Stream
With theStream
.Charset = "UTF-8"
.Mode = adModeReadWrite
.Type = adTypeText
.Open
.LoadFromFile "c:\users\domta\desktop\sample.txt"
theContents = .ReadText
.Close
End With
Late Binding
For late binding, no reference needs to be set. However you'll need to use the CreateObject method to create an instance of the Stream object, and read the contents of the file.
Dim theStream As Object
Dim theContents As String
Set theStream = CreateObject("ADODB.Stream")
With theStream
.Charset = "UTF-8"
.Mode = 3 'adModeReadWrite
.Type = 2 'adTypeText
.Open
.LoadFromFile "c:\users\domta\desktop\sample.txt"
theContents = .ReadText
.Close
End With
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论