0

How can I open a tab delimited .txt file in Excel from VB.NET code?

This question has been asked on many forums, but I can not find an answer anywhere that actually solves this problem.

Dim fileName As String = "file.txt"
Dim filePath As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim fullFilePath As String = filePath.Substring(0, filePath.LastIndexOf("\"c)) & "\" & fileName

Public Sub OpenFileInExcel()

    Process.Start("excel.exe", fullFilePath)

End Sub
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bernoulli Lizard
  • 537
  • 1
  • 12
  • 23
  • Just look for an example on reading a text file line by line and then use the split function with tab as the delimiter. – Synaps3 Oct 10 '13 at 21:57
  • I know how to open and read it in the vb program. I want the file opened in Excel. – Bernoulli Lizard Oct 11 '13 at 13:12
  • I know that I can open Excel, open the .txt file, choose tab delimited, and click finish. But I don't want the user to do this, I want the vb program to open it up automatically. – Bernoulli Lizard Oct 11 '13 at 13:16

2 Answers2

1

Just use Process.Start() and pass "excel.exe" as the first parameter, and the filename as the second parameter:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim TabDelimitedFileName As String = "C:\Users\Mike\Documents\somefile.txt"
    If System.IO.File.Exists(TabDelimitedFileName) Then
        Process.Start("excel.exe", Chr(34) & TabDelimitedFileName & Chr(34))
    Else
        MessageBox.Show(TabDelimitedFileName, "File Not Found")
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • This does not work. I provided the full file path and name for my text file as the second parameter, but when Excel opens I get the error "sorry we couldn't find C:users\UserName\Documents\Visual.xlsx. Is it possible it was moved, renamed, or deleted?" Why is Excel trying to look for a made up file in a different location than the one that I specified? – Bernoulli Lizard Oct 11 '13 at 13:14
  • The only thing that I can see that is different is that your file is in the documents folder. Does this matter, and if so how can I open a file that is not in the documents folder? I definetly specified the correct path, but Excel is still looking in the wrong place. – Bernoulli Lizard Oct 11 '13 at 17:49
  • See what happens with the updated code above. I added a check for the file and also enclosed it in quotes before using it with Process.Start(). – Idle_Mind Oct 11 '13 at 18:08
  • That worked, thanks. The file name was already a string, but apparently Excel needs quotes around the actual variable as well. Do you know if there is any way to make the column width autosize when it opens up? – Bernoulli Lizard Oct 11 '13 at 19:12
  • I don't know of any way to do that using just Process.Start(), sorry. If you actually create an instance of Excel thru COM automation I'm sure it could be done, but I personally don't know how to do it that way. – Idle_Mind Oct 11 '13 at 21:13
  • This works, but if excel isn't installed on the computer you'll get the File Not Found message, which isn't correct. If it fails to open the file in excel, I'd suggest making it attempt to open the file in Notepad, and only then display the 'File not found' error if notepad couldn't open it either. – ShadowLiberal Oct 13 '17 at 15:57
0

You can use an Excel COM object to open the file and autofit the columns:

   Dim X As New Microsoft.Office.Interop.Excel.Application()
            X.Workbooks.Open(FileNm)
            Dim W As Microsoft.Office.Interop.Excel.Worksheet = X.ActiveSheet
            Dim R As Microsoft.Office.Interop.Excel.Range = W.Range(W.Cells(1, 1), W.Cells(65000, 250))
            R.Columns.AutoFit()
            X.Visible = True
David A Stumpf
  • 753
  • 5
  • 13