Reputation: 2438
I've been trying for a while to declare or to open a excel sheet in vb.net. I already read excel file in vb.net and other links but it doesn't work.
I added Microsoft Excel 12.0 Object Library. I included:
Imports Microsoft.VisualBasic
Imports System.Net.Mime.MediaTypeNames
Imports Microsoft.Office.Interop
I want to declare / open the excel file in a module:
Public Module postleitzahlen_array
Dim myarray As String
Dim xlApp As Excel.Application
xlApp = New Excel.ApplicationClass ' here is the error, XlApp "has to be declared"
Can someone help me?
EDIT:
Okay, i noticed that i use excel 2007, and there is a difference - now I'm using follwing code from http://vb.net-informations.com/excel-2007/vb.net_excel_2007_create_file.htm
Sub test()
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("Excel file created , you can find the file c:\")
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
but I get an error in xlWorkSheet = xlWorkBook.Sheets("sheet1")
saying "(Exception by HRESULT: 0x8002000B (DISP_E_BADINDEX))
Edit2: I use a german excel, so "sheet1" throws an error --> "tabelle1" is the right word :)
Upvotes: 1
Views: 9882
Reputation: 5805
I wrote a function for getting a workbook in VB.NET that will attempt to get a handle on any running Excel process and only opens a new Excel instance when it can't find one.
''' <summary>
''' Open an Excel file and get a handle to the object.
''' If you want to quit Excel later use: `thisWorkbook.Application.Quit`
''' </summary>
''' <param name="fileName">full path to the workbook you want to open.</param>
''' <returns>The open Excel Workbook handle.</returns>
Public Function OpenExcelFile(fileName As String) As Excel.Workbook
Dim thisWorkbook As Excel.Workbook
Dim excelApp As Excel.Application
#If DEBUG Then
excelApp = New Excel.Application With {
.Visible = True,
.EnableEvents = False ' Suppress the Workbook_Open event
}
#Else
Try ' To attach to running process
excelApp = Marshal.GetActiveObject("Excel.Application")
Catch ex As Exception
' Open Excel if it isn't already
excelApp = New Excel.Application With {
.Visible = False, ' Ninja mode
.EnableEvents = False, ' Suppress the Workbook_Open event
.DisplayAlerts = False ' Supress messages
}
End Try
#End If
Try
Dim excelBooks As Excel.Workbooks = excelApp.Workbooks
thisWorkbook = excelBooks.Open(fileName)
Catch ex As Exception
MsgBox("Error opening Excel file: " & fileName & vbCrLf & ex.Message)
End Try
Return thisWorkbook
End Function
Upvotes: 0
Reputation: 4897
As for your error, substituting ApplicationClass
to simply Application
had solved my problem.
Upvotes: 1