Automating OpenFileDialog to Open a file without user input
Hello I have a windows forms program in vb.net that uses a datagrid to
pull a dataset (excel) and display its contents. Right now I'm having
trouble setting up a refresh that will dynamically update the dataset by
pressing a button. The only way I think I might do this is to automate
Excel to close and then open up the file without the user having to go
through an open dialog control. How might I be able to automate my code to
achieve this? I greatly appreciate any help you may offer.
Sub refreshDataSet()
Try
Cursor.Current = Cursors.WaitCursor
Write2myExcel()
Finally
Cursor.Current = Cursors.Default
End Try
closexlsfile()
If openExcelfile(txtFilePath.Text) Then
Try
Cursor.Current = Cursors.WaitCursor
RetrieveExcel()
ValidateExcel()
Finally
Cursor.Current = Cursors.Default
End Try
Me.btnBrowse.Enabled = False
End If
End Sub
Function openExcelfile(programmedfilename as string) As Boolean
openExcelfile = False
If programmedfilename <> "" Then
Dim dlg As New OpenFileDialog()
dlg.Filter = "Excel Macro Enabled Files|*.xlsm*|Excel
Files|*.xls|Excel 2007 Files|*.xlsx|All Files|*.*"
If dlg.ShowDialog() = DialogResult.OK Then
txtFilePath.Text = dlg.FileName
End If
Else
txtFilePath.Text = programmedfilename
End If
Me.ExcelWriteBtn.Enabled = txtFilePath.Text.Length > 0
Me.closexlsbtn.Enabled = txtFilePath.Text.Length > 0
Dim ExcelSheetName As String = ""
objExcel = CreateObject("Excel.Application")
wrkbks = objExcel.Workbooks
objworkbook = wrkbks.Open(programmedfilename)
openExcelfile = True
End Function
No comments:
Post a Comment