CreateObject vs New

  • Thread starter Thread starter dR
  • Start date Start date
D

dR

Since PowerPoint is a single instance application, what is the
difference between:

Set oPPApp = New PowerPoint.Application

and

Set oPPApp = CreateObject("PowerPoint.Application")



Thanks
 
Since PowerPoint is a single instance application, what is the
difference between:

Set oPPApp = New PowerPoint.Application

and

Set oPPApp = CreateObject("PowerPoint.Application")

One fires off a new instance of PowerPoint and gives you a reference to it or
gives you a reference to the already-running instance if one exists.

The other gives you a reference to the already-running instance of PPT if one
exists or fires off a new instance and gives you reference to it.

IOW, no real difference.
 
One fires off a new instance of PowerPoint and gives you a reference to itor
gives you a reference to the already-running instance if one exists.

The other gives you a reference to the already-running instance of PPT if one
exists or fires off a new instance and gives you reference to it.

IOW, no real difference.

-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ:  www.pptfaq.com
PPTools:  www.pptools.com
================================================

If there is no real difference, and a new instance will be started as
necessary,is there any sense in testing for an existing instance?

I saw many users using code like this:

On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0

If oPPApp Is Nothing Then
Set oPPApp = New PowerPoint.Application
Else
'** Do nothing. PowerPoint already exists.
End If

Is there any purpose to all this?

Thanks
dR
 
dR said:
If there is no real difference, and a new instance will be started as
necessary,is there any sense in testing for an existing instance?

I saw many users using code like this:

On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
On Error GoTo 0

If oPPApp Is Nothing Then
Set oPPApp = New PowerPoint.Application
Else
'** Do nothing. PowerPoint already exists.
End If

Is there any purpose to all this?

Thanks
dR

You would want to close the PowerPoint instance that you started and not the
one that you did not start. So, you typically, would write code like this:

On Error Resume Next

bNewPPApp = False
Set oPPApp = GetObject(, "PowerPoint.Application")

If oPPApp Is Nothing Then
bNewPPApp = True
Set oPPApp = New PowerPoint.Application
End If

In close routine:

If bNewPPApp Then
oPPApp.Quit
End If

Also, for the instance that you started and user opened a new presentation
in it, you don't want to close that instance too. Check for presentations
that exist and were not opened by your app in PowerPoint before closing that
instance of PowerPoint.

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
You would want to close the PowerPoint instance that you started and not the
one that you did not start.

I'd say the same thing but put it slightly differently:

If there's already an instance of PPT running, the user's doing something with
PPT already; you don't want to close PPT when you're done. Otherwise, you know
that you created the instance, so it's fairly certain that it's ok to close it.

(If the user starts working with PPT while you're automating it, all bets are
off, of course)

In other words, you can't tell the difference between an instance you started
and one you did not once the instance is started, but if you find that there's
already an instance when you know you didn't create it, you can be sure it's
not yours.


So, you typically, would write code like this:
 
Back
Top