|
-
Jan 10th, 2004, 03:06 PM
#1
Thread Starter
New Member
Open Excel From VB (to display a text file)
I have a tab-delimited text-file which I want to allow user to view in Excel. How can i Do that?
I had a few thoughts:
1) rename the text file to .xls(i used ms-dos). Then I could open the file from VB using GetObject(). Excel opens fine. But problem is dunno how to copy or rename files from VB (instead of going manually to ms-dos)
2) open excel, wkbk, wksheets and populate data one-by-one using recordset. But i realise that that when using recordset, we need to know the flds involve....my text file is a matrix (rows and columns) data file...and i have no knowledge of the size...thus can't know how many flds involve.
Any help is appreciated.
3) or is there a way to open text file in excel (control from VB of cos) automatically??
Thanks in advance....
-
Jan 13th, 2004, 12:33 PM
#2
Junior Member
you can try this
(with including Microsoft Excel x.x Object Library)
VB Code:
Sub MakeXL(csvPath As String, NewXLPath As String, NewXLSheetName As String, Delimiter As String)
Dim wb As Workbook, ws As Worksheet
Set wb = Workbooks.Add: Set ws = wb.Worksheets.Add
ws.Name = NewXLSheetName
ff = FreeFile
Open csvPath For Input As ff
On Error Resume Next
Do While Not EOF(ff)
Line Input #ff, buff
n = n + 1
buff = Split(buff, Delimiter)
If IsArray(buff) Then
For c = 0 To UBound(buff)
ws.Range(Chr(c + 65) & n) = buff(c)
If Err Then
'if the first char of the item is -,+,= or etc. an error occures
ws.Range(Chr(c + 65) & n) = "'" & buff(c)
'so we add a ' sign to the first char
End If
Next c
Else
ws.Range(Chr("A" & n)) = buff
End If
If Err Then MsgBox "Error on col:" & c + 1 & " row:" & n & vbCrLf & Err.Description
Err.Number = 0
Loop
Close #ff
wb.Close True, NewXLPath
End Sub
i tried the code and it ok
-
Jan 13th, 2004, 01:19 PM
#3
Thread Starter
New Member
Thanks a mill....it works!!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|