Board index » Visual Studio » testing whether an object exists

testing whether an object exists

Visual Studio378
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...

But VB gives me an error on this.

Do I need to make error trapping to test whether an object exists or are

there other ways???

Regards

Peter


-
 

Re:testing whether an object exists

"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl

Quote
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...



if myopject Is Nothing Then



--

Reply to the group so all can participate

VB.Net... just say "No"



-

Re:testing whether an object exists

"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl

Quote
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...



sorry, hit SEND too soon...



If myopject Is Nothing Then

-or-

If Not (myopject Is Nothing) Then



-

Re:testing whether an object exists

On Mon, 24 May 2004 18:17:07 +0200, "Peter" <->wrote:



Quote
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...

But VB gives me an error on this.

Do I need to make error trapping to test whether an object exists or are

there other ways???



If MyObject Is Nothing Then



or



If Not (MyObject Is Nothing) Then



The parentheses are optional but I always use them when I use "Not".



Gale.



-

Re:testing whether an object exists

Hi

Thanks!!!

haven't known that the solution is so easy.

Regards

Peter



"Peter" <->schrieb im Newsbeitrag

Quote
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...

But VB gives me an error on this.

Do I need to make error trapping to test whether an object exists or are

there other ways???

Regards

Peter









-

Re:testing whether an object exists

Bob and others:



I was going to ask this very thing when I saw this post. I set objects to

the Excel application (objXL), a workbook (objWkbk), and a worksheet in that

workbook (objWS). I wanted to write something that would shut down my VB

app if the user closes the workbook directly rather than using the Exit

button on the toolbar, which would close the workbook and the app.



I set up a Timer event and, using your guidelines here, I tried this:

If objWkbk Is Nothing Then

Unload Form1

Else

MsgBox objWkbk.Name

End If



As long as the workbook is open, I get the MsgBox. I closed the workbook,

and got a VB error message box - "An object has disconnected from its

clients" - rather than shutting down the app.



How can I detect if this object no longer exists and use it?



Ed



"Bob Butler" <tiredofit@nospam.com>wrote in message

Quote
"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl

>Hi

>

>I thought that I can do the following to test whether an object still

>exists:

>if myopject<>Nothing then...



sorry, hit SEND too soon...



If myopject Is Nothing Then

-or-

If Not (myopject Is Nothing) Then







-

Re:testing whether an object exists

"Ed" <Ed_Millis@NOSPAM.Hotmail.com>wrote in message

Quote
Bob and others:



I was going to ask this very thing when I saw this post. I set

objects to the Excel application (objXL), a workbook (objWkbk), and a

worksheet in that workbook (objWS). I wanted to write something that

would shut down my VB app if the user closes the workbook directly

rather than using the Exit button on the toolbar, which would close

the workbook and the app.



I set up a Timer event and, using your guidelines here, I tried this:

If objWkbk Is Nothing Then

Unload Form1

Else

MsgBox objWkbk.Name

End If



As long as the workbook is open, I get the MsgBox. I closed the

workbook, and got a VB error message box - "An object has

disconnected from its clients" - rather than shutting down the app.



How can I detect if this object no longer exists and use it?



You have a reference to the workbook and Excel has a reference to the

workbook; when Excel closes out it's reference yours is unaffected so it

will not be "nothing". You can't use it any more because Excel is no longer

managing it but the object still exists out in memory. Always remember that

you can nver set an object to Nothing -- you set a reference to an object to

Nothing.



In your case I think you have 2 options:

1) trap the error in VB and handle it by releasing the references you have

2) use a module-level variable for the Excel Application and Workbook

objects and use WithEvents. You will thne be able to select the excel

references in the left-hand dropdown over the code window and pick events

for them from the right-hand drop down.



Private WithEvents moWB As Excel.Workbook



Private Sub moWB_BeforeClose(Cancel As Boolean)

' the workbook is being closed so release my reference

set moWB=Nothing

End Sub





--

Reply to the group so all can participate

VB.Net... just say "No"



-

Re:testing whether an object exists

I understood this -



"Bob Butler" <tiredofit@nospam.com>wrote in message

Quote
You have a reference to the workbook and Excel has a reference to the

workbook; when Excel closes out it's reference yours is unaffected so it

will not be "nothing". You can't use it any more because Excel is no

longer

managing it but the object still exists out in memory. Always remember

that

you can nver set an object to Nothing -- you set a reference to an object

to

Nothing.



My *VB* object still exists - it just doesn't have anything to connect to

anymore, which is what the error message was telling me.



I got lost here, though:



Quote
1) trap the error in VB and handle it by releasing the references you have



I thought that's what I was trying to do with IsError. But it raised the

error and *didn't* run through the If condition.



This sounded like a much better option:



Quote
2) use a module-level variable for the Excel Application and Workbook

objects and use WithEvents. You will thne be able to select the excel

references in the left-hand dropdown over the code window and pick events

for them from the right-hand drop down.



Private WithEvents moWB As Excel.Workbook



Private Sub moWB_BeforeClose(Cancel As Boolean)

' the workbook is being closed so release my reference

set moWB=Nothing

End Sub



Right now, I set the objects with:

' Open file

Set objXL = CreateObject("Excel.Application")

Set objWkbk = objXL.Workbooks.Open(strShell, ReadOnly:=True)

objXL.Visible = True

Set objWS = objWkbk.Sheets("Sheet1")



If I set this object and reference at the module level, then does it replace

the objWkbk everywhere else? Or is it addition to it, just set to reference

the same workbook?



Ed





-

Re:testing whether an object exists

"Ed" <Ed_Millis@NOSPAM.Hotmail.com>wrote in message

<cut>

Quote
I got lost here, though:



>1) trap the error in VB and handle it by releasing the references

>you have



I thought that's what I was trying to do with IsError. But it raised

the error and *didn't* run through the If condition.



If you mean with "Is Nothing" then it may be what you were trying for but

doesn't work. You'd need to wrap all references to the workbook with error

handling



On Error Goto eh

Set objWS = objWkbk.Sheets("Sheet1")

<other code>



eh:

' clean up (may want to check Err.Number first)

set objWS=Nothing

set objWkbk=Nothing





Quote
This sounded like a much better option:

<cut>

Right now, I set the objects with:

' Open file

Set objXL = CreateObject("Excel.Application")

Set objWkbk = objXL.Workbooks.Open(strShell, ReadOnly:=True)

objXL.Visible = True

Set objWS = objWkbk.Sheets("Sheet1")



If I set this object and reference at the module level, then does it

replace the objWkbk everywhere else? Or is it addition to it, just

set to reference the same workbook?



How you set the references isn't important. How you declare them is. At

the module level you want:

Private WithEvents objXL As Excel.Application

Private WithEvents objWkbk As Excel.Workbook

Private WithEvents objWS As Excel.Worksheet



Note that you have to have a reference to Excel to use this method; late

binding won't work. (You can still use CreateObject though)



--

Reply to the group so all can participate

VB.Net... just say "No"



-

Re:testing whether an object exists

I don't know if your familiar with SQL, but onething that you can't do in

SQL is compare a NULL with a equals sign. You have to do it like column IS

NULL or column IS NOT NULL. Similarly happens in VB: object Is Nothing/Not

(object Is Nothing).



-Wes



"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl...

Quote
Hi



I thought that I can do the following to test whether an object still

exists:

if myopject<>Nothing then...

But VB gives me an error on this.

Do I need to make error trapping to test whether an object exists or are

there other ways???

Regards

Peter









-

Re:testing whether an object exists

Thanks, Bob. This is a lot of stuff for me to wrap my brain around, so I'll

chew on it to get a better grasp. If I can't get it, I'll holler back. But

I really appreciate the time you've spent explaining things.



Ed



"Bob Butler" <tiredofit@nospam.com>wrote in message

Quote
"Ed" <Ed_Millis@NOSPAM.Hotmail.com>wrote in message

news:ujYX97dQEHA.3596@tk2msftngp13.phx.gbl

<cut>

>I got lost here, though:

>

>>1) trap the error in VB and handle it by releasing the references

>>you have

>

>I thought that's what I was trying to do with IsError. But it raised

>the error and *didn't* run through the If condition.



If you mean with "Is Nothing" then it may be what you were trying for but

doesn't work. You'd need to wrap all references to the workbook with

error

handling



On Error Goto eh

Set objWS = objWkbk.Sheets("Sheet1")

<other code>



eh:

' clean up (may want to check Err.Number first)

set objWS=Nothing

set objWkbk=Nothing





>This sounded like a much better option:

<cut>

>Right now, I set the objects with:

>' Open file

>Set objXL = CreateObject("Excel.Application")

>Set objWkbk = objXL.Workbooks.Open(strShell, ReadOnly:=True)

>objXL.Visible = True

>Set objWS = objWkbk.Sheets("Sheet1")

>

>If I set this object and reference at the module level, then does it

>replace the objWkbk everywhere else? Or is it addition to it, just

>set to reference the same workbook?



How you set the references isn't important. How you declare them is. At

the module level you want:

Private WithEvents objXL As Excel.Application

Private WithEvents objWkbk As Excel.Workbook

Private WithEvents objWS As Excel.Worksheet



Note that you have to have a reference to Excel to use this method; late

binding won't work. (You can still use CreateObject though)



--

Reply to the group so all can participate

VB.Net... just say "No"







-