PowerPoint Automation

The following comes from the Usenet group: Microsoft.Public.Office.developer.vba

Hello Howard Kaikow,
I have modified a little bit your procedure. As result I get same process
could you try it and see what happens.
I get same windows handle and processID for both PowerPoint instances

Note: I am with PowerPoint XP

Thank you for cooperation

Galin Iliev
MCSD

Option Explicit

Private Declare Function GetWindowThreadProcessId Lib “user32″ (ByVal hwnd
As Long, lpdwProcessId As Long) As Long
Private Declare Function GetForegroundWindow Lib “user32.dll” () As Long
Public Sub MultiplePowerpoints()
Dim appPowerpoint As PowerPoint.Application
Dim appPowerpointAnother As PowerPoint.Application
Dim appWord As Word.Application
Dim blnPPTRunning As Boolean
Dim lFirstID As Long
Dim lSecondID As Long
Dim lFirstHandle As Long
Dim lSecondHandle As Long

On Error Resume Next
Set appPowerpoint = GetObject(Class:=”PowerPoint.Application”)
If appPowerpoint Is Nothing Then
Powerpoint not running, create instance of Powerpoint
blnPPTRunning = False
Err.Clear
Set appPowerpoint = New PowerPoint.Application
MsgBox “Creating first instance of Powerpoint“, vbOKOnly, _
Powerpoint was not already running”
Else
blnPPTRunning = True
MsgBox “Using running instance of Powerpoint“, vbOKOnly, “Powerpoint
was already running”
End If
With appPowerpoint
.Visible = True
.Activate
lFirstHandle = GetForegroundWindow()
.WindowState = ppWindowMinimized
End With
‘ Create another instance of Powerpoint
Set appPowerpointAnother = New PowerPoint.Application
If appPowerpointAnother Is Nothing Then
With Err
MsgBox .Number & “: ” & .Description, vbOKOnly, “Could not
create second instance of Powerpoint
End With
Else
MsgBox “OK!”, vbOKOnly, “Created second instance of Powerpoint
With appPowerpointAnother
.Visible = True
.Activate
lSecondHandle = GetForegroundWindow()
.WindowState = ppWindowMinimized
End With
End If

‘Check processes IDs
GetWindowThreadProcessId lFirstHandle, lFirstID
GetWindowThreadProcessId lSecondHandle, lSecondID
If lFirstID = lSecondID Then MsgBox “appPowerpoint and appPowerpoint
hold same process!”

‘If this code started PowerPoint
If blnPPTRunning = False Then
If vbYes = MsgBox(”Select Yes to kill the First instance of
Powerpoint“, vbYesNo, _
Powerpoint hit squad needs your instructions”) Then
appPowerpoint.Quit
End If
End If

If vbYes = MsgBox(”Select Yes to kill the second instance of
Powerpoint“, vbYesNo, _
Powerpoint hit squad needs your instructions”) Then
appPowerpointAnother.Quit
End If

‘ Create another instance of Word
‘ Do not kill
Set appWord = New Word.Application
If appWord Is Nothing Then
With Err
MsgBox .Number & “: ” & .Description, vbOKOnly, “Could not
create second instance of Word”
End With
Else
MsgBox “OK!”, vbOKOnly, “Created second instance of Word”
With appWord
.Visible = True
.WindowState = wdWindowStateMinimize
End With
End If

Set appPowerpoint = Nothing
Set appPowerpointAnother = Nothing
Set appWord = Nothing

End Sub


You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

AddThis Social Bookmark Button

Leave a Reply