2

I have the below code to replace tabs with commas in a text document:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Path:\CSV.txt", ForReading)

strText = objFile.ReadAll
strTab = vbTab

strText = Replace(strText, strTab, ",")

objFile.Close

Set objFile = objFSO.OpenTextFile("Path:\CSV.txt", ForWriting)

objFile.Write strText

objFile.Close

This works fine on a unicode text document that I create myself, however throws up an error when used on a unicode text document created from excel.

Line: 16 Char: 1 Error: Invalid procedure call or argument Code:800A0005

Here is an example of a text file created from excel that gives the error:

CSV.txt

Can anyone see what is different about this text document that would be causing the error?

EDIT: I have just tried writing the result to a new text document instead of overwriting the same one, but this has just given a garbled result in the document:

ÿþa 黐멸ȯ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 , , , , , 

 , , , , , , , 

 , , , , , , , 

EDIT2: the suggestion of using TristateTrue from 'Siddharth Rout' gets me past the initial 'Invalid procedure call' error, but now i'm stuck with the garbled Chinese character output - Can anyone advise as to how to fix this?

2
  • Could you tell us which is Line 16? Commented Aug 11, 2016 at 12:35
  • 2
    Line 16 is objFile.Write strText Commented Aug 11, 2016 at 12:37

1 Answer 1

2

Replace

Set objFile = objFSO.OpenTextFile("Path:\CSV.txt", ForWriting)

with

Kill "Path:\CSV.txt"
Set objFile = objFSO.CreateTextFile("Path:\CSV.txt", True, True)

Now try it

Sign up to request clarification or add additional context in comments.

5 Comments

Did you replace "Path:\CSV.txt" with the relevant path?
Yes I have replaced the path and I have put it on two lines as in your post.
Any idea why it doesn't for me? I have just tried writing the result to a new text file instead of overwriting the same one, but this has just given a garbled result. (see edit to original post)
Try with Set objFile = objFSO.OpenTextFile("Path:\CSV.txt", ForReading) with TristateTrue as shown Here
This gets me past the initial 'Invalid procedure call' error, but now i'm stuck with the garbled Chinese character output...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.