
HTML OPEN EXCEL FILE IN NEW WINDOW HOW TO
The project builds without error.Ĭan anyone suggest why I get this syntax error and how to correct it?įirst place the import statements at the top of the form or code module you are working in Imports Excel = Microsoft.OfficeĬode to open Excel (read the MessageBox within the code) Public Sub OpenExcelDemo(ByVal FileName As String, ByVal SheetName As String) If IO.File.Exists(FileName) Then Dim Proceed As Boolean = False Dim xlApp As Excel.Application = Nothing Dim xlWorkBooks As Excel.Workbooks = Nothing Dim xlWorkBook As Excel.Workbook = Nothing Dim xlWorkSheet As Excel.Worksheet = Nothing Dim xlWorkSheets As Excel.Sheets = Nothing Dim xlCells As Excel.Range = Nothing xlApp = New Excel.Application xlApp.DisplayAlerts = False xlWorkBooks = xlApp.Workbooks xlWorkBook = xlWorkBooks.Open(FileName) xlApp.Visible = True xlWorkSheets = xlWorkBook.Sheets For x As Integer = 1 To xlWorkSheets.Count xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet) If xlWorkSheet.Name = SheetName Then Console.WriteLine(SheetName) Proceed = True Exit For End If (xlWorkSheet) xlWorkSheet = Nothing Next If Proceed Then xlWorkSheet.Activate() MessageBox.Show("File is open, if you close Excel just opened outside of this program we will crash-n-burn.") Else MessageBox.Show(SheetName & " not found.") End If xlWorkBook.Close() xlApp.UserControl = True xlApp.Quit() ReleaseComObject(xlCells) ReleaseComObject(xlWorkSheets) ReleaseComObject(xlWorkSheet) ReleaseComObject(xlWorkBook) ReleaseComObject(xlWorkBooks) ReleaseComObject(xlApp) Else MessageBox.Show("'" & FileName & "' not located.

When I call the procedure, I get "Incorrect Syntax Near 'Microsoft'." I have commented the entire procedure except for the first Dim statement:īut I still get the error. XlsWB = xlsWorkbooks.Open(Session("strExcelFilePath")) ' To call: Session("strExcelFilePath") = ExcelFilePathĭim xlsApp As Excel.Application = Nothingĭim xlsWorkBooks As Excel.Workbooks = Nothing ' Purpose : This routine displays the Excel spreadsheet at location strPath.

The procedure that is trying to open the file looks like this: Public Sub DisplayExcelFile()

I'm trying to open an Excel workbook from VB.Net.
