Hi,
I'm new to OLE Automation. Can anyone show me how to:
1. Open an existing Excel file
2. Insert an empty row
3. Enter data in that row from my VB app.
4. Save the modified excel file
As always, thanks
Printable View
Hi,
I'm new to OLE Automation. Can anyone show me how to:
1. Open an existing Excel file
2. Insert an empty row
3. Enter data in that row from my VB app.
4. Save the modified excel file
As always, thanks
Hi Tomexx !!!
Set a reference to excel (menuitem project-> references)
Use the following code:
hope this help a little bit.Code:Sub main()
'init the symbols
Dim objExcel As Excel.Application 'init excel
Dim wbkWorkbook As Excel.Workbook 'init workbook
Dim wksSheet As Excel.Worksheet 'init worksheet
'set reference to new Excel
Set objExcel = New Excel.Application
' set a reference to workbook and open it
Set wbkWorkbook = objExcel.Workbooks.Open("c:\mydir\myfile.xls")
'set a reference to a worksheet - in this case the name is
'myworksheet
Set wksSheet = wbkWorkbook.Worksheets("myworksheet")
'only for viewing
objExcel.Visible = True
'enter in cell A1 the value hugo
With wksSheet.Range("a1")
.Value = "hugo"
End With
'close and save the workbook
wbkWorkbook.Close savechanges:=true
'quit excel
objExcel.Quit
'destroy any references
Set wksSheet = Nothing
Set wbkWorkbook = Nothing
Set objExcel = Nothing
-cu TheOnly
Thanks TheOnly but..
I was thinking about using something like:
Can I do it this way instead of setting references to Excel like you showed me?Code:Dim objExcel as object
Set objExcel = CreateObject("Excel.Sheet")
objExcel.Application.Cells(1, 1).Value = Text1
objExcel.Application.Cells(1, 5).Formula = "=Sum(D2:D30)"
objExcel.Application.save= "test.xls"
etc.
Thanks
Hi Tomexx !!!
Yes you can !!! The difference is early Binding (my version) or late binding (yours).
There are some other issues why I use early binding.
e.g. coding - in the IDE you can use the
methodes/properties like vb ones.
For the diffrence of early/late binding search this forum.
-cu TheOnly