Solved

Create UTF8 text file

Posted on 2008-10-29
1
1,087 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 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

828 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