How To Read .Txt File With Chinese characters In VBA?


Recently, I was working on a project where I attempted to process text files containing Chinese language content, only to find that the characters were not being interpreted correctly by my VBA code.

The root of this issue lies in the character encoding used by the text file and how VBA handles non-ASCII characters. VBA typically relies on the system's default character encoding, which may not support languages with non-Latin characters like Chinese.

In this blog post, I will explore reading .txt files with Chinese characters in VBA. We'll solve the problem, understand why it occurs, and look into potential solutions to ensure the processing of Chinese text files within our VBA projects.

When we faced with the challenge of reading .txt files containing Chinese characters in VBA, we have applied several strategies to ensure proper interpretation and processing of the text. Let's check some possible solutions:

  1. By default, VBA may use the system's default character encoding, which may not support Chinese characters. We can explicitly specify the character encoding when opening the text file to ensure proper interpretation of Chinese characters. For example, we can use the UTF-8 encoding, which supports a wide range of characters including Chinese.
  2. Use ADODB.Stream Object: Another approach is to use the ADODB.Stream object to read the text file. This object provides greater flexibility and control over the reading process, allowing us to specify the character encoding explicitly. We can load the contents of the text file into the ADODB.Stream object and then read the text using the Stream's ReadText method.
  3. Handle Non-ASCII Characters: When processing the text read from the file, we need to ensure that our VBA code can handle non-ASCII characters such as Chinese characters. We may need to use Unicode-aware functions and methods to manipulate the text effectively.

Code exampe, how we can read a .txt file with Chinese characters in VBA:


        Sub ReadChineseTextFile()
            Dim filePath As String
            Dim fileContents As String
            
            ' Specify the file path
            filePath = "C:\filefolder\file.txt"
            
            ' Open the text file with UTF-8 encoding
            Open filePath For Input As #1
            fileContents = Input$(LOF(1), #1)
            Close #1
            
            ' Process the file contents
            MsgBox fileContents
        End Sub
    

In above code, we explicitly specify UTF-8 encoding when opening the text file using the Open statement. This ensures that Chinese characters are interpreted correctly, then read the contents of the file into a string variable and process it as needed.


2

As we explore solutions for reading strings from text files with Chinese characters in VBA, an alternative approach involves using the ADODB.Stream object. Here's how we can implement this solution:

Firstly, we need to ensure that the .Charset property of the ADODB.Stream object is set to the charset of the file we want to read. This ensures that the stream interprets the characters correctly. We can set the .Charset property to "UTF-8" or any other appropriate charset.

To utilize the ADODB.Stream object, we must add the reference "Microsoft ActiveX Data Objects Library" in the VBA Menu under Extras › References.


        Dim adoStream As ADODB.Stream
        Set adoStream = New ADODB.Stream

        adoStream.Charset = "UTF-8" ' Set the correct charset
        adoStream.Open
        adoStream.LoadFromFile FilePathForReading

        LstStr = adoStream.ReadText

        adoStream.Close
        Set adoStream = Nothing
    

Here we create a new instance of the ADODB.Stream object and set its .Charset property to "UTF-8". We then open the stream, load the file contents from the specified FilePath, read the text using the ReadText method, and finally close the stream.