Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- Public Class Form1
- <DllImport("User32")> _
- Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
- End Function
- <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
- Private Shared Function FindWindow( _
- ByVal lpClassName As String, _
- ByVal lpWindowName As String) As IntPtr
- End Function
- Private Const mconXLWindowClass As String = "XLMAIN"
- Private mXL As Object
- Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
- Dim OFD As OpenFileDialog
- OFD = New OpenFileDialog()
- With OFD
- .CheckFileExists = True
- .CheckPathExists = True
- .Filter = "Test files (*.txt)|*.txt|Excel spreadsheets (*.xls, *.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*"
- .FilterIndex = 2
- .InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
- .RestoreDirectory = True
- .Title = "Select Data File"
- If .ShowDialog = DialogResult.OK Then
- txtPath.Text = .FileName
- OpenExcel(tpDisplay.Handle, True)
- 'OpenExcel(True)
- End If
- End With
- End Sub
- Private Sub OpenExcel(ByVal bShow As Boolean)
- OpenExcel(IntPtr.Zero, bShow)
- End Sub
- Private Sub OpenExcel(ByVal hwnd As IntPtr, ByVal bShow As Boolean)
- Dim hwndExcel As IntPtr
- Try
- mXL = CreateObject("Excel.Application")
- If hwnd <> IntPtr.Zero Then
- hwndExcel = FindWindow(mconXLWindowClass, mXL.Caption)
- SetParent(hwndExcel, hwnd)
- End If
- mXL.Visible = bShow
- OpenFile()
- Catch ex As Exception
- MsgBox(ex.Message)
- End Try
- End Sub
- Private Sub OpenFile()
- Dim objWorkbooks As Object
- objWorkbooks = mXL.Workbooks
- objWorkbooks.Open(txtPath.Text)
- While (System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbooks) > 0)
- End While
- objWorkbooks = Nothing
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement