Is it possible to and edit an excel file from Visual Basic 2005?
-
I have written much code in VBA and now I wanted to explore Visual Basic 2005. My objective is to be able to open an excel file and to plug in a value in range ("A1") in the excel worksheet/workbook from a Visual Basic 2005 form. Any help/sample code would be tremendously appreciated. Thank you, Gray
-
I have written much code in VBA and now I wanted to explore Visual Basic 2005. My objective is to be able to open an excel file and to plug in a value in range ("A1") in the excel worksheet/workbook from a Visual Basic 2005 form. Any help/sample code would be tremendously appreciated. Thank you, Gray
Hope this helps, make sure you add your references.
'Open in Excel Dim myExcel As New Microsoft.Office.Interop.Excel.Application() Dim workBook As Microsoft.Office.Interop.Excel.Workbook Dim workSheet As New Worksheet Try If myExcel Is Nothing Then 'Failed to load Excell Object Else workBook = myExcel.Workbooks.Open("C:\myfile.xls", 0, True, 5, "", "", True, XlPlatform.xlWindows, "\t", False, False, 0, True, 1, 0) workSheet = workBook.ActiveSheet workSheet.Cells(1, 1) = "My value" workBook.Close() End If Catch ex As Exception 'Handle Exception End Try myExcel.Close()
-
Hope this helps, make sure you add your references.
'Open in Excel Dim myExcel As New Microsoft.Office.Interop.Excel.Application() Dim workBook As Microsoft.Office.Interop.Excel.Workbook Dim workSheet As New Worksheet Try If myExcel Is Nothing Then 'Failed to load Excell Object Else workBook = myExcel.Workbooks.Open("C:\myfile.xls", 0, True, 5, "", "", True, XlPlatform.xlWindows, "\t", False, False, 0, True, 1, 0) workSheet = workBook.ActiveSheet workSheet.Cells(1, 1) = "My value" workBook.Close() End If Catch ex As Exception 'Handle Exception End Try myExcel.Close()
Silly me, I forgot to save the document. To do this, use workSheet.Save or workSheet.SaveAs(fileName).
-
Silly me, I forgot to save the document. To do this, use workSheet.Save or workSheet.SaveAs(fileName).
Thank you so much Justin! It works and I couldn't be more thankful. Again, thank you very much.
-
I have written much code in VBA and now I wanted to explore Visual Basic 2005. My objective is to be able to open an excel file and to plug in a value in range ("A1") in the excel worksheet/workbook from a Visual Basic 2005 form. Any help/sample code would be tremendously appreciated. Thank you, Gray
Microsoft Office Interop has loads of good stuff. Justin's example is just the beginning of it.
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon