Solved

Create UTF8 text file

Posted on 2008-10-29
1
1,085 Views
Last Modified: 2012-05-05
I want to create an UTF8 text file from data that I have in my application.

I simply need a function that will take string as a parameter, convert it to UTF8 and then create UTF8 text file containing the string.
0
Comment
Question by:JiriNovotny
1 Comment
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 22838017
try this module:


Public Enum ForWriteEnum
   ForWriting = 2
   ForAppending = 8
End Enum
#If False Then  'PreserveEnumCase
   Public ForWriting, ForAppending
#End If

Public Enum TristateEnum
   TristateTrue = -1        'Opens the file as Unicode
   TristateFalse = 0        'Opens the file as ASCII
   TristateUseDefault = -2  'Use default system setting
End Enum
#If False Then  'PreserveEnumCase
   Public TristateTrue, TristateFalse, TristateUseDefault
#End If

Public Function getUnicodeValue(v As String) As String
    Dim sUniText As String, I As Long
    For I = 1 To Len(v) Step 2
    'Someone in EE said use For I = 1 To I < Len(v) Step 2 ?
    'http://www.experts-exchange.com/Microsoft/Development/Microsoft_Programming/Q_23652503.html?cid=236#a22242933
        sUniTex = sUniTex & ChrW(Asc(Mid$(v, I + 1, 1)) * 256& + Asc(Mid$(v, I, 1)))
    Next
    getUnicodeValue = sUniTex
End Function

Public Sub writeUnicodeFile( _
   ByVal sFileName As String, _
   ByVal vVar As Variant, _
   Optional ByVal ForWrite As ForWriteEnum = ForWriting, _
   Optional ByVal TriState As TristateEnum = TristateTrue, _
   Optional ByVal bJoin As Boolean)

   Dim objFSO           As Object
   Dim objStream        As Object
   Dim sText            As String

   Set objFSO = CreateObject("Scripting.FileSystemObject")
   If (Not objFSO Is Nothing) Then
      Set objStream = objFSO.OpenTextFile( _
         sFileName, ForWrite, True, TriState)

      If (Not objStream Is Nothing) Then
         With objStream
            If bJoin Then
               sText = Join(vVar, vbCrLf)
            Else
               sText = vVar
            End If
            .Write sText
            .Close
         End With
         Set objStream = Nothing
      End If
      Set objFSO = Nothing
   End If
End Sub



then use like:

writeUnicodeFile "filepath...", "your utf-8 text"
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now