Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word VBA - read text file - accented characters

Posted on 2011-02-15
10
Medium Priority
?
1,654 Views
Last Modified: 2012-05-11
Word 2007 VBA

I am reading a text file using the code below and storing in a string variable. However accented characters are coming in different to what they should be.

What can I do?

TIA

I'll try to included example but may not render correctly
Telefónica -> Telefónica

Function GetText(sFile As String) As String
    Dim nSourceFile As Integer, sText As String
    Dim lngChars As Long

    ''Close any open text files
    Close
    
    ''Get the number of the next free text file
    nSourceFile = FreeFile
    
    ''Write the entire file to sText
    Open sFile For Input As #nSourceFile
    lngChars = LOF(nSourceFile)
    sText = Input(lngChars, nSourceFile)
   
    Close
    GetText = sText
    
End Function

Open in new window

0
Comment
Question by:Philip Pinnell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:CreepyD
ID: 34897892
It may not be the ultimate solution, but this might work.
Here's something I did in vbscript, which I would assume would work for VBA as well.

Basically, read the file into a String as you are doing.
Then use the Replace() function, copying and pasting the incorrect characters into it, and replacing them with the correct accented characters.
Then I simply write the new String (including replaced characters) back to the file - Or you can do what ever with that string.

Remember this is vbscript, not VBA, but it should be almost identicle.


Set objFSO = CreateObject("Scripting.FileSystemObject")
File = "C:\Users\gbeajmn_sa\Desktop\all.txt"
Set objFile = objFSO.OpenTextFile(File, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "ó", "ó") 'Replace with correct character here
Set objFile = objFSO.OpenTextFile(File, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Open in new window

0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34897933
Thanks

Yeah, but it is a bit of a bodge. I don't know what all the translations should be. I really want a more robust and "proper" solution.
0
 
LVL 4

Expert Comment

by:jmdion
ID: 34899843
Can you post a sample text file?  It looks like the input may be 16-bit characters and the VBA code expects 8-bit characters.
0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34900519
Not in work til Thursday.

> It looks like the input may be 16-bit characters and the VBA code expects 8-bit characters.

If that is the case what would I do?
0
 
LVL 4

Expert Comment

by:jmdion
ID: 34900946
Not sure what would work.  I would try first functions like StrConv with the vbFromUnicode argument. Or other VBA functions that work on strings and that come up in a google search with "ANSI", "UTF", "UCS", "Unicode" or "encoding".
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 34900960
You could try reading it in as a document.
Dim doc As Document
Set doc = Documents.Open(FileName:=sFile, Format:=wdOpenFormatUnicodeText)
sText = doc.Range.Text

Open in new window

0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34901013
What is your ultimate goal here?  In other words, after importing the text, where will it be used?  The thing about text files is that they are just text -- there is little or no support for accented characters.  If you are just reusing it somewhere else in Word, I would just copy a range and then paste it somewhere else, using the paste option that preserves formatting.
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34909418
It is pasted as html into a content control of a word document.

The problem is that it is wrong as soon as I get it from the file.

I will try the opening as document suggestion tomorrow
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34916243
Hi

The opening as a document seems to work, but

It gets garbled later on.

Not my code but what happens is we have a fragment of HTML and this is palced on the clipboards using windows functions

            CopyMemory ByVal lpData, ByVal sData, Len(sData)
            GlobalUnlock hMemHandle
            EmptyClipboard
            SetClipboardData m_cfHTMLClipFormat, hMemHandle

The fragment is in sData and is still correct. It is copied to memory then SetClipboardData puts it on the clipboard.

The clipboard contents are pasted to the content control using
Selection.PasteSpecial , , , , WdPasteDataType.wdPasteHTML

If I try to get the string onto the clipboard any other way so far I can't use the HTML paste format.

I'll have to post another question I guess, but that is where I am
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 34916290
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

670 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