Solved

Create UTF8 text file

Posted on 2008-10-29
1
1,083 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 49

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

705 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

12 Experts available now in Live!

Get 1:1 Help Now