Search code examples
vbautf-8powerpointutf

Displaying accented characters using VBA in PowerPoint


I have created a PowerPoint to display 5 random words from a .txt file following this excellent tutorial.

The problem is, the words in my .txt file are Spanish, so have accents on them. When PowerPoint displays them, they look corrupt. For example Perú looks like Perð.

This is my code:

Public myArray, Word1, Word2, Word3, Word4, Word5

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)

If SSW.Presentation.SlideShowSettings.StartingSlide Then
Randomize
Label1.Caption = ""
Label2.Caption = ""
Label3.Caption = ""
Label4.Caption = ""
Label5.Caption = ""

Dim path
path = ActivePresentation.path & "\palabras.txt"

Open path For Input As #1
filecontent = Input(LOF(1), #1)
Close #1

myArray = Split(filecontent, vbCrLf)

End If

End Sub


Private Sub CommandButton1_Click()

Word1 = Int((UBound(myArray)) * Rnd)
Word2 = Int((UBound(myArray)) * Rnd)
Word3 = Int((UBound(myArray)) * Rnd)
Word4 = Int((UBound(myArray)) * Rnd)
Word5 = Int((UBound(myArray)) * Rnd)

Do While Word1 = Word2
Word2 = Int((UBound(myArray)) * Rnd)
Loop

Label1.Caption = myArray(Word1)
Label2.Caption = myArray(Word2)
Label3.Caption = myArray(Word3)
Label4.Caption = myArray(Word4)
Label5.Caption = myArray(Word5)


End Sub

I know the end of it is messy too, I didn't know how to get it so that Word3, 4 and 5 didn't repeat. It's my first time using VBA.

Can anyone help?

Files


Solution

  • VBA and COM use Unicode internally. But when interacting with the Windows API VBA uses ANSI as Windows 9x didn't have Unicode API calls.

    The Open statement is depreciated. It will be calling Windows' CreateFileA. Using the FileSystemObject. https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/openastextstream-method

    Make sure you specify Unicode when opening the file.

    OR set your non Unicode settings to Spanish or something.

       Const ForReading = 1, ForWriting = 2, ForAppending = 8
       Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
       Dim fso, f, ts
       Set fso = CreateObject("Scripting.FileSystemObject")
       fso.CreateTextFile "test1.txt"   ' Create a file.
       Set f = fso.GetFile("test1.txt")
       Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
       ts.Write "Hello World"
       ts.Close
       Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
       TextStreamTest = ts.ReadLine
       ts.Close
    

    From Help.


    Set TS = CreateObject("Scripting.FileSystemObject").GetFile("test1.txt").OpenAsTextStream(1, -1)
    x = ts.readall