View Single Post
Old 02-02-2004, 08:31
Alex's Avatar
Alex Alex is offline
Join Date: Nov 2002
Posts: 1,941
Blog Entries: 4
Default Re: is application running?

First question: how do you connect to Photoshop? Do you use GetObject method in VBA? If so, I would use CreateObject instead because this is supposed to launch the application if it is not already running (provided that Adobe implemented this correctly).

If this doesn't work, then probably the following code might help:

Option Explicit

    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Const INFINITE = &HFFFF      '  Infinite timeout

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, _
                                ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
                                ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
                                ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Sub EnsurePhotoshopRunning()
    Dim hWnd As Long
    Dim nRet As Long
    hWnd = FindWindow("Photoshop", vbNullString)
    If hWnd = 0 Then
        ' Photoshop is NOT running. Launch it.
        si.cb = Len(si)
        nRet = CreateProcess(vbNullString, "C:\Program Files\Adobe\Photoshop\Photoshop.exe", 0, 0, 0, 0, 0, vbNullString, si, pi)
        If nRet = 0 Then
            MsgBox "Unable to launch Adobe Photoshop", vbCritical
            WaitForInputIdle pi.dwProcessId, INFINITE
            CloseHandle pi.hProcess
            CloseHandle pi.hThread
        End If
    End If
End Sub
Basically the way it works is quite straightforward. First it looks for Photoshop window (a window with "Photoshop" class which is the one belongs to Adobe Photoshop). If not found, it starts the application by launching it, waits till the application initializes and ready and then exits.

Make sure you have the correct application path in CreateProcess method.
Reply With Quote