Home > Archive > Visual Basic > February 2006 > Checking for exististing objects
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Checking for exististing objects
|
|
| David De Bono 2006-02-27, 6:56 pm |
| Hi,
I'm using the Word object, like:
Set objWord = Word.Application
If this fails I use the New keyword. In the IDE this is annoying since I
want the program to halt on errors to debug, but not on this error. Ia there
any way to check if I need to use create the object insted of just
connecting to it ?
In my case if Word is already running.
And is there any way of chosing halt on all errors or ignore errors in
different places ?
David
| |
| Karl E. Peterson 2006-02-27, 6:56 pm |
| David De Bono wrote:
> And is there any way of chosing halt on all errors or ignore errors in
> different places ?
You can ignore errors very easily, with something like this:
On Error Resume Next
' Do a bunch of stuff...
On Error Goto 0
Later... Karl
--
Working without a .NET?
http://classicvb.org/
| |
|
| David,
You can do something like this:
On Error GoTo errHandler:
'Create a Word App object
Set objWord = CreateObject("Word.Application")
errHandler:
Select Case Err.Number
Case 429
MsgBox "Please install Office to use this functionality.",
vbCritical
End Select
"David De Bono" <er_fortsatt@hotmail.com> wrote in message
news:eLjpok%23OGHA.3196@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> I'm using the Word object, like:
>
> Set objWord = Word.Application
>
> If this fails I use the New keyword. In the IDE this is annoying since I
> want the program to halt on errors to debug, but not on this error. Ia
> there any way to check if I need to use create the object insted of just
> connecting to it ?
>
> In my case if Word is already running.
>
> And is there any way of chosing halt on all errors or ignore errors in
> different places ?
>
> David
>
| |
| David De Bono 2006-02-27, 6:56 pm |
| Hi,
I think I did not explain this correctly. Under Options in the IDE you can
set Halt on all errors since I want the code to stop when error. I have an
error handler who takes care of this in the exe file.
David
In the IDE you can
"Jones" <Jones@isjonesingforspam.com> wrote in message
news:OKubgr%23OGHA.2088@tk2msftngp13.phx.gbl...
> David,
>
> You can do something like this:
>
> On Error GoTo errHandler:
>
> 'Create a Word App object
> Set objWord = CreateObject("Word.Application")
>
> errHandler:
> Select Case Err.Number
> Case 429
> MsgBox "Please install Office to use this functionality.",
> vbCritical
> End Select
>
> "David De Bono" <er_fortsatt@hotmail.com> wrote in message
> news:eLjpok%23OGHA.3196@TK2MSFTNGP09.phx.gbl...
>
>
| |
|
|
"David De Bono" <er_fortsatt@hotmail.com> wrote in message
news:eLjpok%23OGHA.3196@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> I'm using the Word object, like:
>
> Set objWord = Word.Application
>
> If this fails I use the New keyword. In the IDE this is annoying since I
> want the program to halt on errors to debug, but not on this error. Ia
> there any way to check if I need to use create the object insted of just
> connecting to it ?
>
> In my case if Word is already running.
That's not how you should be doing this. Try this (air code):
On Error Resume Next
'Try to get a reference to an instance of an existing Word.Application
object
Set objWord = GetObject("Word.Application")
On Error GoTo EH 'resume normal error handling
If objWord Is Nothing Then
'No reference so apparently Word is not running; create a new instance
Set objWord = New Word.Application
End If
Note that using New Word.Application requires early-binding. For
late-binding, declare the object variable As Object and use CreateObject, as
such
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
--
Mike
Microsoft MVP Visual Basic
| |
| David De Bono 2006-02-27, 6:56 pm |
| Hi,
See my previous reply.
David
"MikeD" <nobody@nowhere.edu> wrote in message
news:eRJnnf$OGHA.1696@TK2MSFTNGP14.phx.gbl...
>
> "David De Bono" <er_fortsatt@hotmail.com> wrote in message
> news:eLjpok%23OGHA.3196@TK2MSFTNGP09.phx.gbl...
>
>
>
> That's not how you should be doing this. Try this (air code):
>
> On Error Resume Next
>
> 'Try to get a reference to an instance of an existing Word.Application
> object
> Set objWord = GetObject("Word.Application")
>
> On Error GoTo EH 'resume normal error handling
>
> If objWord Is Nothing Then
> 'No reference so apparently Word is not running; create a new instance
> Set objWord = New Word.Application
> End If
>
> Note that using New Word.Application requires early-binding. For
> late-binding, declare the object variable As Object and use CreateObject,
> as such
>
> Dim objWord As Object
> Set objWord = CreateObject("Word.Application")
>
>
> --
> Mike
> Microsoft MVP Visual Basic
>
>
>
>
| |
| Ken Halter 2006-02-28, 6:55 pm |
| "David De Bono" <er_fortsatt@hotmail.com> wrote in message
news:%23tclVc$OGHA.3100@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> I think I did not explain this correctly. Under Options in the IDE you can
> set Halt on all errors since I want the code to stop when error. I have an
> error handler who takes care of this in the exe file.
>
> David
The settings under Tools/Options can be temporarily changed by
right-clicking a code window, select 'Toggle'. That submenu has breakpoints,
bookmarks and error settings.
btw, if there's already an instance of word running, you can use GetObject
instead of CreateObject or New to get to that instance.
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Freeware 4 color Gradient Frame? http://www.vbsight.com/GradFrameCTL.htm
|
|
|
|
|