|
-
Nov 25th, 2000, 10:38 AM
#1
Thread Starter
Hyperactive Member
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
-
Nov 25th, 2000, 10:53 AM
#2
Addicted Member
Hi Tomexx !!!
Set a reference to excel (menuitem project-> references)
Use the following code:
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
hope this help a little bit.
-cu TheOnly
-
Nov 25th, 2000, 11:53 AM
#3
Thread Starter
Hyperactive Member
Thanks TheOnly but..
I was thinking about using something like:
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.
Can I do it this way instead of setting references to Excel like you showed me?
Thanks
-
Nov 26th, 2000, 07:53 AM
#4
Addicted Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|