Hi
This is my first time posting. I'm new to VB. Been using VBA for a while quite successfully. Done some programming in Java 10 years ago. Thought I'd have ago at VB using the latest Visual Studio 2019.
I want to open an excel workbook and put data into cells. I've tried lots of different ways from various online sources. Just can't get any of it to work. This is what I have at the moment, but it errors on the line Book = oExcel.workbooks.Add
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.workbooks.Add
'Add data to cells of the first worksheet in the new workbook
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Doe"
oSheet.Range("B2").Value = "John"
'Save the Workbook and Quit Excel
oBook.SaveAs("F:\Book1.xlsx")
oExcel.Quit
End Sub
This is my first time posting. I'm new to VB. Been using VBA for a while quite successfully. Done some programming in Java 10 years ago. Thought I'd have ago at VB using the latest Visual Studio 2019.
I want to open an excel workbook and put data into cells. I've tried lots of different ways from various online sources. Just can't get any of it to work. This is what I have at the moment, but it errors on the line Book = oExcel.workbooks.Add
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel
oExcel = CreateObject("Excel.Application")
oBook = oExcel.workbooks.Add
'Add data to cells of the first worksheet in the new workbook
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Doe"
oSheet.Range("B2").Value = "John"
'Save the Workbook and Quit Excel
oBook.SaveAs("F:\Book1.xlsx")
oExcel.Quit
End Sub