Solved

Vb generated Excel object not destroyed

Posted on 2000-03-20
7
302 Views
Last Modified: 2008-02-01
Hi All,

I am having a problem with an excel 9.0 object I create using VB6 w/ SP3 on NT4 w/ SP6.  I am putting data into an excel spreadsheet object and then generating a chart from that data.  This is all cool.  I am terminating all my objects properly, but an excel.exe process is still hanging there.  Dunno why it won't die.  Anyone done this before and experienced this problem.  Please help.

Here some code:

    Dim strChartType As String
   
        Dim lngRow As Long
    Dim lngCol As Long
    Dim lngRowTotal As Long
    Dim lngColTotal As Long
    Dim intFreeFile As Integer
    Dim strRow As String
   
    Dim dblData As Double
    Dim dbltest As Double
    Dim intNull As Integer
   
    lngRowTotal = objChart.RowCount
    lngColTotal = objChart.ColumnCount

    Dim objExcel As Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objExcelRange As Excel.Range
    Dim objWorksheet As Excel.Worksheet


    Set objExcel = New Excel.Application
       
    objExcel.Visible = True

    Set objWorkbook = objExcel.Workbooks.Add

    Set objWorksheet = objWorkbook.Worksheets.Add
 
       
        ' Write Column Headers (space first)
        objChart.Row = 1
        strRow = ""
        For lngCol = 1 To lngColTotal
            objChart.Column = lngCol

            objWorksheet.Cells(1, lngCol + 1).Value = objChart.ColumnLabel
        Next lngCol


        ' Write Data
        For lngRow = 1 To lngRowTotal
            objChart.Row = lngRow
            strRow = ""
            'Y axis data
            objWorksheet.Cells(lngRow + 1, 1).Value = objChart.RowLabel
            For lngCol = 1 To lngColTotal
                objChart.Column = lngCol
               
                GetData lngRow, lngCol, dblData, intNull
                If intNull = 0 Then
                    'X Axis data
                    objWorksheet.Cells(lngRow + 1, lngCol + 1).Value = dblData
                End If
            Next lngCol
        Next lngRow

     ' Auto dynamically gets range of data
    Set objExcelRange = objWorkbook.Worksheets(1).Range("a1").CurrentRegion
   
   
    Select Case objChart.ChartType
        Case 0  ' 3D Bar
            strChartType = xl3DColumnClustered
        Case 1  ' 2D Bar
            strChartType = xlBarClustered
        Case 2  ' 3D Line
            strChartType = xl3DLine
        Case 3  ' 2D Line
            strChartType = xlLine
        Case 4  ' 3D Area
            strChartType = xl3DArea
        Case 5  '2D Area
            strChartType = xlArea
        Case 16 '2D XY
            strChartType = xlXYScatterLines
       
    End Select
   
    With objExcel
        Charts.Add
        ActiveChart.ChartType = strChartType

        ActiveChart.SetSourceData Source:=objExcelRange, PlotBy:=xlColumns

        ActiveChart.Location Where:=xlLocationAsNewSheet
        With ActiveChart
            .HasTitle = True
            .ChartTitle.Characters.Text = objChart.TitleText
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = objChart.Plot.Axis(xlValue, xlPrimary).AxisTitle.Text
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = objChart.Plot.Axis(xlCategory, xlPrimary).AxisTitle.Text
       
        End With
        ActiveChart.HasLegend = True
        ActiveChart.Legend.Select
        Selection.Position = xlBottom
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    End With

    objWorkbook.Close False
   
    objExcel.Quit
   
    Set objExcelRange = Nothing
    Set objWorksheet = Nothing
    Set objWorkbook = Nothing
    Set objExcel = Nothing
0
Comment
Question by:Mdang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:georgeman
ID: 2637765
Try to Close all Excel objects and set to Nothing all Excel objects.
Regards
George
0
 
LVL 1

Expert Comment

by:pclement
ID: 2637818
This looks familiar, so I'll post the same answer as I did for your question on DevX. Try setting objChart to Nothing.

If all else fails and you're certain that you are destroying all instantiated objects, try the following:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
Private Const WM_QUIT = &H12

Sub TerminateExcel()

Dim lpClassName As String
Dim iHwnd As Long
Dim iReturn As Long

lpClassName = "XLMain"
iHwnd = FindWindow(lpClassName, 0&)

If iHwnd Then
    iReturn = PostMessage(iHwnd, WM_QUIT, 0&, 0&)
End If

End Sub

0
 
LVL 9

Expert Comment

by:samopal
ID: 2637835
Try this :

.....    
    Set objExcelRange = Nothing
    Set objWorksheet = Nothing
    Set objWorkbook = Nothing

objExcel.Quit
    Set objExcel = Nothing

HTH


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Mdang
ID: 2637982
I found out the answer.  A chart object was being created by the excel object. (Charts.Add) So I created a new chart object and assigned the chart to it. and it works!  Thanks for all your feedback sorry for not giving anyone points.

set objexcelchart = objexcel.charts.add
0
 

Author Comment

by:Mdang
ID: 2637983
This question has a deletion request Pending
0
 
LVL 1

Expert Comment

by:pclement
ID: 2638423
This question no longer is pending deletion
0
 
LVL 1

Accepted Solution

by:
pclement earned 75 total points
ID: 2638424

Um, yes Michael that was in my comment above. ;-)

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month6 days, left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question