Results 1 to 4 of 4

Thread: OLE Automation and Excel... please

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    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
    Thanks

    Tomexx.

  2. #2
    Addicted Member
    Join Date
    Oct 2000
    Location
    Vienna/Austria
    Posts
    132
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458
    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
    Thanks

    Tomexx.

  4. #4
    Addicted Member
    Join Date
    Oct 2000
    Location
    Vienna/Austria
    Posts
    132
    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
  •  



Click Here to Expand Forum to Full Width