Open Excel From a Windows Form

ViRi

Member
Joined
Jun 19, 2007
Messages
8
Programming Experience
3-5
Hi

I need to Open an Excel File and am using the following code. I get no errors but nothing happens the File in question does not open. I have included a reference to Excel in my Project References.

VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xlTemp As Excel.Application
        xlTemp = New Excel.Application
        xlTemp.Workbooks.Open("C:\Documents and Settings\TAP\Desktop\Post\Test.xls")
    End Sub
End Class


If on the other hand I use the code below, the Excel File will open but only in Read Only Mode. I need to open the File in Normal view

VB.NET:
'Function
Private Sub CallProcess(ByVal path As String)
        Dim p As New System.Diagnostics.Process
        p.StartInfo.FileName = path
        p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
        p.Start()

        'Wait until the process passes back an exit code 
        'p.WaitForExit()

        'Free resources associated with this process
        p.Close()
    End Sub

'Call
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CallProcess("C:\testy.xls")

    End Sub

Thanks ViRi
 
If you don't need to send info to Excel then you don't have to add a reference to start a file. Just use Process.Start("path to and name of file"). This will start your file. But it will not automate it.
 
Back
Top