Results 1 to 4 of 4

Thread: VB app doesn't release control of Excel.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    VB app doesn't release control of Excel.

    I wrote a VB app that reads data from the COM ports and puts it into an array. Then it dumps the data to excel. When I'm done and want to view the data, I double-click the file and Excel doesn't fully launch. If I close my program, then it works. No matter what I try, unless my VB app is closed, any of the files that were created by my app won't load. Here's the parts of my code that have references to creating an Excel object.

    Code:
    Dim objExcel As New Excel.Application
    Dim objBook As New Excel.Workbook
    
    dlgCommon.DialogTitle = "Save Data to File"
          dlgCommon.Filter = "Excel Files(*.xls)|*.xls"
          dlgCommon.ShowSave
          Filename = dlgCommon.Filename
          Set objBook = objExcel.Workbooks.Add
          objExcel.Cells(1, 1) = "Force"
          objExcel.Cells(2, 1) = "(" & lblUnits.Caption & ")"
          objExcel.Cells(1, 3) = Date
          objExcel.Range("C:C").ColumnWidth = 9.5
             For i = 1 To Index
                objExcel.Cells(i + 3, 1) = DataArray(i)
             Next i
    What do I have to add so I don't have to close my app first to view my data? The only thing I have in the Form_Unload is:
    objExcel.Quit

    Which doesn't seem to make a difference.

  2. #2
    Hyperactive Member Scott Penner's Avatar
    Join Date
    Dec 2000
    Location
    Mountain View
    Posts
    327

    kill the reference

    You might try killing the reference to the application from VB.

    That is set ObjBook and ObjExcel = nothing.

    That should work...
    -scott
    he he he

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
          Set objBook = objExcel.Workbooks.Add
          objExcel.Cells(1, 1) = "Force"
          objExcel.Cells(2, 1) = "(" & lblUnits.Caption & ")"
          objExcel.Cells(1, 3) = Date
          objExcel.Range("C:C").ColumnWidth = 9.5
             
          For i = 1 To Index
               objExcel.Cells(i + 3, 1) = DataArray(i)
          Next i
        
          objExcel.Workbooks(1).Save
          objExcel.Quit
          Set objExcel = nothing
    You might just wanna look up the WITH statement also

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    I added the Set objExcel = Nothing
    That took care of it, Thanks!

    I was looking into that b4 but I must not have had it right.

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