Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Read longer string


Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2012, 10:10 PM
tokrot tokrot is offline
Newcomer
 
Join Date: Nov 2008
Posts: 19
Default 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,
Reply With Quote
  #2  
Old 06-25-2012, 01:57 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default

There is not really a limit in the line length when reading a file.

Excel itself has some limits:
http://office.microsoft.com/en-us/ex...005199291.aspx

How do you put the data in a cell?
Reply With Quote
  #3  
Old 06-25-2012, 10:39 PM
richard233 richard233 is offline
Newcomer
 
Join Date: Dec 2009
Posts: 1
Default 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
Reply With Quote
  #4  
Old 06-26-2012, 01:08 AM
tokrot tokrot is offline
Newcomer
 
Join Date: Nov 2008
Posts: 19
Default

Quote:
Originally Posted by Flyguy View Post
There is not really a limit in the line length when reading a file.

Excel itself has some limits:
http://office.microsoft.com/en-us/ex...005199291.aspx

How do you put the data in a cell?
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...
Attached Files
File Type: txt demo.txt (25.3 KB, 6 views)
Reply With Quote
  #5  
Old 06-26-2012, 07:19 AM
MPi MPi is offline
Contributor

Forum Leader
* Expert *
 
Join Date: Dec 2001
Location: Quebec
Posts: 710
Default

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Č
Reply With Quote
  #6  
Old 06-26-2012, 09:38 AM
MPi MPi is offline
Contributor

Forum Leader
* Expert *
 
Join Date: Dec 2001
Location: Quebec
Posts: 710
Default

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Č
Reply With Quote
  #7  
Old 07-04-2012, 10:28 PM
tokrot tokrot is offline
Newcomer
 
Join Date: Nov 2008
Posts: 19
Thumbs up

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..
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->