Andrew Crofts
asked on
Word VBA - read text file - accented characters
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
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
ASKER
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.
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.
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.
ASKER
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?
> 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?
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".
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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
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
ASKER
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.wdPasteHTM L
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
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.wdPasteHTM
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
ASKER
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.
Open in new window