Help on closing object properly

  • Thread starter Thread starter Snedker
  • Start date Start date
S

Snedker

I've made an application in VB 6.0. From this I do the following:

1.
Open specific Excel-file

2.
Automatically insert data into the now open Excel, based on values in
the VB-application.

3.
Cut all bonds to the Excel-application, but leave it open for further
work and saving.

4.
It should be possible to close the Excel file doing the above several
times.

This is what I use to open:

---code begin
Dim xlo As excel.Application
Dim xloW As excel.Workbook

Dim myCount As Integer, x As Integer, NewMeasure As Integer
Dim isEven As Boolean

If xlo Is Nothing Then
Set xlo = New excel.Application
Else
Set xlo = excel.Application
End If
Set xloW = xlo.Workbooks.Open("u:\excel\kontrolkort.xls")
xloW.Application.Visible = True
...
---code end

Currently I have a "Set xlo = Nothing" at the end. But if I close
Excel, Excel is not visible but I can see it as a running process. If
I attempt to open it again through my VB-app it errors.

Am I approacihing it wrongly?



Regards /Snedker
 
Set xlo = Nothing just clears the variable.
Try

xlo.Quit

If I understand what you're trying to do, that should be what you want.
- Pikus
 
Hi,
When Excel remains visible in the task list it is usualy because you've used
an Excel global object.

Ex: MyValue=range("a1").value will work from VB6 but only because "range" is
a global object exposed by Excel's object model.
AKA implicit object.
Ex: MyValue=xlo.range("a1").value is an explicit object and will be set to
nothing when xlo is.

On your second point if you set xlo=nothing or close the Excel session the
only way to "reconnect" is to use getobject(). Re-running Set xlo = New
excel.Application will alway create a NEW session & NEW object.

See getobject() in VB6 help for a code example to test for a running Excel
object or create a new object if Excel is not running.
 
Set xlo = Nothing just clears the variable.
Try

xlo.Quit

If I understand what you're trying to do, that should be what you want.

xlo.Quit makes the Excel application want to quit. I wish to keep
working in it...?
 
Or xloW.Close then.

Maybe I'm just not understanding what you're trying to close and why...
- Pikus
 
Back
Top