Board index » Visual Studio » testing whether an object exists
|
swammie
|
|
swammie
|
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 - |
| Bob
Registered User |
Mon May 24 11:21:05 CDT 2004
Re:testing whether an object exists
"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl
QuoteHi -- Reply to the group so all can participate VB.Net... just say "No" - |
| Bob
Registered User |
Mon May 24 11:22:07 CDT 2004
Re:testing whether an object exists
"Peter" <->wrote in message news:e4zrMqaQEHA.2520@TK2MSFTNGP11.phx.gbl
QuoteHi If myopject Is Nothing Then -or- If Not (myopject Is Nothing) Then - |
| Gale
Registered User |
Mon May 24 11:21:51 CDT 2004
Re:testing whether an object exists
On Mon, 24 May 2004 18:17:07 +0200, "Peter" <->wrote:
QuoteHi or If Not (MyObject Is Nothing) Then The parentheses are optional but I always use them when I use "Not". Gale. - |
| Peter
Registered User |
Mon May 24 13:23:36 CDT 2004
Re:testing whether an object exists
Hi
Thanks!!! haven't known that the solution is so easy. Regards Peter "Peter" <->schrieb im Newsbeitrag QuoteHi - |
| Ed
Registered User |
Mon May 24 15:04:53 CDT 2004
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 - |
| Bob
Registered User |
Mon May 24 15:17:25 CDT 2004
Re:testing whether an object exists
"Ed" <Ed_Millis@NOSPAM.Hotmail.com>wrote in message
QuoteBob and others: 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" - |
| Ed
Registered User |
Mon May 24 17:34:28 CDT 2004
Re:testing whether an object exists
I understood this -
"Bob Butler" <tiredofit@nospam.com>wrote in message QuoteYou have a reference to the workbook and Excel has a reference to the anymore, which is what the error message was telling me. I got lost here, though: Quote1) trap the error in VB and handle it by releasing the references you have error and *didn't* run through the If condition. This sounded like a much better option: Quote2) use a module-level variable for the Excel Application and Workbook ' 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 - |
| Bob
Registered User |
Mon May 24 19:04:14 CDT 2004
Re:testing whether an object exists
"Ed" <Ed_Millis@NOSPAM.Hotmail.com>wrote in message
<cut> QuoteI got lost here, though: 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 QuoteThis sounded like a much better option: 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" - |
| Wes
Registered User |
Mon May 24 22:10:48 CDT 2004
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... QuoteHi - |
| Ed
Registered User |
Tue May 25 08:31:14 CDT 2004
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 - |
