I am trying to run two auto macros (in different files) one after the other where the run times are unknown. Both macros conclude with "application.quit" I need a function which will allow the second macro start immediately after the first has closed Excel. Is there any means in vba of determining if excel is already open when starting the second file ? Something like "wait if application already open" ?
Any ideas welcome!
Thanks
Astrikor
Last edited by astrikor; 12-21-2010 at 04:56 AM.
Hello astrikor,
This macro will allow you to start another application, and resume running your macro after the application closes.
'Written: November 09, 2007 'Author: Leith Ross 'Summary: Starts an Application and waits till it has finished loading before ' running the remaining macro code. Private Declare Function OpenProcess _ Lib "kernel32.dll" _ (ByVal dwAccess As Long, _ ByVal fInherit As Integer, _ ByVal hObject As Long) As Long Private Declare Function WaitForSingleObject _ Lib "kernel32.dll" _ (ByVal hHandle As Long, _ ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle _ Lib "kernel32.dll" _ (ByVal hObject As Long) As Long Sub StartAndWait(ByVal AppPath As String, ByVal Appname As String) Const SYNCHRONIZE As Long = 1048576 Const INFINITE As Long = -1& Dim Obj As Object Dim ProcessID As Long Dim ProcessHandle As Long Dim Retval As Long Dim Services As Object Set Services = GetObject("winmgmts:root\cimv2") Set Obj = Services.Get("Win32_Process") Retval = Obj.Create(Appname, AppPath, , ProcessID) If Retval Then ProcessError (Retval) GoTo CleanUp End If If ProcessID <> 0 Then ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID) Retval = WaitForSingleObject(ProcessHandle, INFINITE) Retval = CloseHandle(ProcessHandle) MsgBox Appname & " Finished Loading", 64 Else MsgBox "ERROR : Unable to start " & Appname End If CleanUp: Set Services = Nothing Set Obj = Nothing End Sub Sub ProcessError(ByVal Err_Value As Long) If Err_Value = 0 Then Exit Sub ProcessError: Select Case Err_Value Case 2 Msg = "Access Denied" Case 3 Msg = "Insufficient Privilege" Case 8 Msg = "Unknown failure" Case 9 Msg = "Path Not Found" Case 21 Msg = "Invalid Parameter" Case Else Msg = "Unknown Error:" & Str(Err_Value) End Select MsgBox Msg, vbexcalamtion, "WMI Win32_Process Failure" End Sub
Macro Example
Sub StartAndWaitTest() StartAndWait "C:\Windows", "Notepad.exe" MsgBox "Notepad has closed." End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thanks Leith,
It's a solution which I have tried and it works, but as it's beyond my understanding I am reluctant to use it.
I was hoping for something simpler - I am aware of the Hauppauge VTPlus Script for example:
exec file="applicationpath filepath" wait exec
This simply waits until the application has been executed and closed before moving on to a subsequent instruction
(which could be repeats of this code using different filepaths)
I could use this approach, but would prefer to stay within excel.
Any comments ?
Astrikor
Hello astrikor,
Excel does not support this feature natively. You will have to use an outside method.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks