 |
 |

06-24-2012, 10:10 PM
|
|
Newcomer
|
|
Join Date: Nov 2008
Posts: 19
|
|
Read longer string
|
Hello everyone,
I was trying to read a chunk of csv file line by line. However, no matter what codes I found internet online, the read back chars would only be limited to 255 or lesser. May I ask whether anyone has any chance to read more than >255 chars per line from a file?
Thanks,
|
|

06-25-2012, 01:57 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,884
|
|
|

06-25-2012, 10:39 PM
|
|
Newcomer
|
|
Join Date: Dec 2009
Posts: 1
|
|
One way to do it.
|
Dim filetoread as string
filetoread=("c:\test.txt")
dim filestream as StreamReader
filestream = File.Opentext(filetoread)
Dim readcontents as String
readcontents = fileStream.ReadToEnd()
Dim textdelimiter as String
textdelimiter = chr(13) 'This is your line delimiter = line feed, it might be vbcrlf
Dim splitLine = Split(readcontents, textdelimiter)
filestream.Close()
textdelimiter = ","
dim i,x as integer
for i=0 to Ubound(splitLine)
splitLine(i) = replace(splitline(i), chr(10),"") ' remove the carriage return
Dim SplitCols = split(splitLine(i), textdelimiter)
Dim Field as string
for x = 0 to Ubound(splitCols)
Field = SplitCols(x)
'Do whatever you need here with the field
next
next
|
|

06-26-2012, 01:08 AM
|
|
Newcomer
|
|
Join Date: Nov 2008
Posts: 19
|
|
Quote:
Originally Posted by Flyguy
|
Thanks for your reply....I use array to put those string in to the cells since the data are separated by commas. However, I wanted to read them in text format. So I actually had them to read in this:
Code:
Dim Data As String, InputFile as String
Dim fSo, myfile
InputFile = YourFileHere
Set fSo = CreateObject("Scripting.FileSystemObject")
Set myfile = fSo.opentextfile(InputFile, 1)
Open InputFile For Random As #1
Data = myfile.read(LOF(1))
Close #1
*I have renamed the csv file to text due to unable to upload....the data read up to 252 chars in this case....
I thank you for your help in advance... 
|
|

06-26-2012, 07:19 AM
|
|
Contributor
Forum Leader * Expert *
|
|
Join Date: Dec 2001
Location: Quebec
Posts: 710
|
|
|
Hello,
Is there any reason to read the file line by line ?
Are you looking for a particular piece of string or something like that ?
|
__________________
MPiČ
|

06-26-2012, 09:38 AM
|
|
Contributor
Forum Leader * Expert *
|
|
Join Date: Dec 2001
Location: Quebec
Posts: 710
|
|
If it's not necessary to read the file line by line, you could copy it in one shot like this
Code:
Sub ImportFile()
Dim vFile As Variant
Dim strTemp As String
Dim FF As Integer
Dim MyDataObject As DataObject 'Needs reference to Microsoft Forms 2.0 Object Library
FF = FreeFile
vFile = Application.GetOpenFilename
If Not vFile Then Exit Sub
Open vFile For Binary As #FF
strTemp = Space$(LOF(FF))
Get #FF, , strTemp
Set MyDataObject = New DataObject
MyDataObject.SetText strTemp
MyDataObject.PutInClipboard
Range("A1").PasteSpecial
Close #FF
MyDataObject.Clear
Set MyDataObject = Nothing
End Sub
|
__________________
MPiČ
|

07-04-2012, 10:28 PM
|
|
Newcomer
|
|
Join Date: Nov 2008
Posts: 19
|
|
Hello Guys,
I want to apologize for the late reply. I finally got everything to work. The reason why I would want to read line instead of using excel is because I want to give the user an option to choose to write the CSV in a text or excel format.
OK, let's go back to how I figure out. The add watch command can only show up to 255 or lesser but in fact if the string variable is to write to a file, it will write the entire string (>255). I'm not sure why there's a limitation in the display of the watch command but I'm glad my codes work. I thanks everyone who has helped and replied in this thread.
Thanks again everyone..  Happy Holidays btw.. 
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|