Question Open Excel Workbook using OFD

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
I've figured out how to open a text file and display the contents in a rich textbox, and how to open a word document using an Open File Dialog,but can't get an excel workbook to open. What I am hoping for is to be able to open any MS application from an Open File Dialog. This is what I have so far:

VB.NET:
Option Explicit On
Option Strict On
Imports System.IO
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop

Public Class frmOpenDocuments

    Private Sub btnOpenWordDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenWordDocument.Click
        Dim _wordObject As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application()

        Me.OpenFileDialog1.Filter = "Word Document|*.doc|All Files|*.*"
        Me.OpenFileDialog1.Title = "Select the Word Document to Open"
        Me.OpenFileDialog1.FileName = ""
        If Me.OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim documentName As Object
            documentName = OpenFileDialog1.FileName
            _wordObject.Documents.Open(documentName)
            _wordObject.Visible = True
            Me.SendToBack()
            Me.OpenFileDialog1.Dispose()
        End If
    End Sub

    Private Sub btnOpenExcelDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenExcelDocument.Click
        Me.OpenFileDialog1.Filter = "Excel WOrkbook|*.xls|All Files|*.*"
        Me.OpenFileDialog1.Title = "Select the Excel WOrkbook to Open"
        Me.OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            If IO.Path.GetExtension(OpenFileDialog1.FileName) = ".xls" Then
                Process.Start(OpenFileDialog1.FileName)
            End If
        End If
    End Sub

    Private Sub btnBrowseTextDocuments_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseTextDocuments.Click
        Me.OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"
        Me.OpenFileDialog1.Title = "Select the Text File to Open"
        Me.OpenFileDialog1.FileName = ""
        'OpenFileDialog1.InitialDirectory = Application.ExecutablePath
        If Me.OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Me.txtFilename.Text = OpenFileDialog1.FileName
            Me.ReadTextFile(txtFilename.Text)
        End If
    End Sub

    Private Sub ReadTextFile(ByVal sFileName As String)
        Dim oSR As System.IO.StreamReader
        oSR = System.IO.File.OpenText(sFileName)
        Me.RichTextBox1.Text = oSR.ReadToEnd()
    End Sub

Any help?
 
Got it right after I posted, like this.

VB.NET:
 Private Sub btnOpenExcelDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenExcelDocument.Click
        Me.OpenFileDialog1.Filter = "Excel WOrkbook|*.xls|All Files|*.*"
        Me.OpenFileDialog1.Title = "Select the Excel WOrkbook to Open"
        Me.OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            If IO.Path.GetExtension(OpenFileDialog1.FileName) = ".xls" Then
                Me.Refresh()
                Me.OpenFileDialog1.Dispose()
                Me.SendToBack()
                System.Diagnostics.Process.Start(OpenFileDialog1.FileName)
            End If
        End If
    End Sub
 
Back
Top