Results 1 to 4 of 4

Thread: Help with Excel

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    30

    Help with Excel

    I searched the entire forum and couldn't get a good answer. I have created an application which stores the information in an Excel file.
    I have created the Excel object inside a form. On the click of a command button an excel file is created and the data is saved. There seems to be no problem if two or more excel file is created. But when I reload that form again and try to create excel file. It gives "_Global failed". The excel application quits only when the program is closed
    The code is

    Dim xls As Object

    Private Sub cmd_Make_Click()
    xls.Workbooks.Add
    xls.Workbooks(1).Activate

    ' Code to write to the Excel sheet
    Worksheets("Sheet1").Cells(1, 1).Value = "Sl. No"

    ActiveWorkbook.SaveAs App.Path & "\Excel\" & strFileName, xlExcel9795
    xls.ActiveWorkbook.Close
    End Sub


    Private Sub Form_Unload(Cancel As Integer)
    xls.Quit
    Set xls = Nothing
    End Sub
    Last edited by cool_coolioin; Dec 13th, 2002 at 08:18 AM.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. 'Why are you not using the Excel object library reference?
    2. [b]Private[/b] xlsQuotation As [b]Excel.Application[/b]
    3.  
    4. 'I take it you've got code already to set the object?
    5. [b]Private Sub form_activate()
    6.     me.show
    7.     set xlsQuotation = new Excel.Application
    8. End Sub[/b]
    9.  
    10. Private Sub cmd_Make_Click()
    11.     ' The above variable is xlsQuotation, yet the below code
    12.     ' references a "xls" named Excel object variable...
    13.     [b]xlsQuotation.[/b]Workbooks.Add
    14.     [b]xlsQuotation.[/b]Workbooks(1).Activate
    15.  
    16.     'You haven't referenced the Excel object that the sheet is part of
    17.     [b]xlsQuotation.[/b]Worksheets("Sheet1").Cells(1, 1).Value = "Sl. No"
    18.     'Ditto Here
    19.     [b]xlsQuotation.[/b]ActiveWorkbook.SaveAs App.Path & _
    20.     "\Excel\" & strFileName, xlExcel9795
    21.     [b]xlsQuotation.[/b].ActiveWorkbook.Close
    22. End Sub
    23.  
    24. Private Sub Form_Unload(Cancel As Integer)
    25.     [b]xlsQuotation.[/b].Quit
    26.     'You haven't put the object name in here
    27.     Set [b]xlsQuotation[/b] = Nothing
    28. End Sub
    Last edited by alex_read; Dec 12th, 2002 at 09:02 AM.

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2001
    Posts
    30
    There has been some typing mistake
    But it doesn't still work. The Excel application is not closed

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    If Excel doesn't close, then you have not set all references to Excel Objects to = Nothing.

    NOTE There are some references that Excel may produce on your behalf (typically wen using .RANGE commands), so you will need to de-bug most carefully.....

    OR if you are really lucky, post all of your code here, and someone may see the error.

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