Bernoulli Lizard Bernoulli Lizard - 1 year ago 77 Question

Open a .txt file in excel from VB.NET

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

Answer Source

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))
        MessageBox.Show(TabDelimitedFileName, "File Not Found")
    End If
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download