Results 1 to 3 of 3

Thread: Automating Excel from VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    1

    Lightbulb Automating Excel from VB

    Hi,

    We are facing some problem while automating excel application with Visual Basic. We are addressing the problem below and want to share your expertise to overcome the problem.

    The requirement was: We need to write to an existing excel file (different sheets) programmatically from database tables and open the excel for view.

    What we did: We have a VB application through that we are trying to write to an existing excel file. The excel file is connected through a DataControl objects with datasource as the excel file and RecordSources as the required sheet names. Now we are writing these RecordSets by invoking edit method. This is writing to the excel file fine. But the problem is we are unable to write to the RecordSets ( for excel) without opening the excel file. So we are opening the excel file using Shell Function. So the processing cycle now became:

    1. Opening the excel file using SHELL.
    2. Loading the form that has excel updating routine. (Excel application goes to the back)
    3. Running the creation process to update excel file
    4. Updating complete
    5. Unloading the Form
    6. Excel application comes to the front
    7. User saves it and closes
    8. Return to main menu

    The process runs fines, but some times even if we close the excel (step 7) it remains in the task and machine hangs.


    dated 14/08/2002
    With the above problem if i can do the editing job without opening the excel file physicvally then there is no question of excel getting hanged.
    Is this possible?
    Last edited by mcc; Sep 14th, 2002 at 12:04 AM.

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    I'm not even clear on what your question is, but here's a way to get at excel that has never caused me any problems. give it a try.
    Attached Files Attached Files

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Try this one ...
    VB Code:
    1. ' Needs a reference to the "Microsoft Excel x.0 Object Library"
    2. ' from the Project menu > References options.
    3. Private objXLApp As Excel.Application
    4.  
    5. Private Sub Form_Load()
    6.  
    7.     ' Use Late Binding Automation rather than the shell statement.
    8.     ' This creates a "behind the scenes" instance of MsExcel.
    9.     Set objXLApp = New Excel.Application
    10.  
    11.     With objXLApp
    12.         ' Add a new workbook, then select the first sheet.
    13.         .Workbooks.Open "C:\Path\File.xls"
    14.         .Workbooks(1).Worksheets(1).Select
    15.  
    16.         ' Using the CopyFromRecordset() procedure built into Excel, paste
    17.         ' your table into the first cell of the first worksheet.
    18.         .Worksheets(1).Range("A1").CopyFromRecordset YourRecordsetVariable
    19.        
    20.         ' Now show MsExcel to the user.
    21.         .Visible = True
    22.        
    23.         ' If you want, you can now save the workbook:
    24.         .Workbooks(1).SaveAs "C:\path\File.xls"
    25.     End With
    26.    
    27. End Sub
    28.  
    29. Private Sub Form_Unload(Cancel As Integer)
    30.  
    31.     ' If the instance of Excel we made is still open when this program is exited,
    32.     ' close it & unload it's reference taken up in the computers memory.
    33.     If Not (objXLApp Is Nothing) Then
    34.         Set objXLApp = Nothing
    35.     End If
    36.  
    37. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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