duckbert
asked on
Switching to app after app.previnstance is called
Hi, I am sure this is fairly easy, but I want to be able to allow my application to switch to the running one when it is loaded again, rather than just displaying a message box with "You can't do that!" I would like to show the application without an error. I currently have this code:
If App.PrevInstance Then
MsgBox "AutoBill is already running, you can only run one version of AutoBill at a time", vbInformation, App.Title
Exit Sub
End If
I want to switch to autobill.
If App.PrevInstance Then
MsgBox "AutoBill is already running, you can only run one version of AutoBill at a time", vbInformation, App.Title
Exit Sub
End If
I want to switch to autobill.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am still a little stumped about why this isn't working.
I entered this as my code
If App.PrevInstance Then
AppActivate App.Title
End
End If
With all my windows having the variable set to app.title
I tried to get both suggestions to work, but I couldn't, I also checked MSDN for app.activate and came up with what I was supplied with. Why isn't this working? and I can't seem to get it to work through any shell idea either. Maybe if I could find the process id of my previous app that could work but I still don't know what to do.
I entered this as my code
If App.PrevInstance Then
AppActivate App.Title
End
End If
With all my windows having the variable set to app.title
I tried to get both suggestions to work, but I couldn't, I also checked MSDN for app.activate and came up with what I was supplied with. Why isn't this working? and I can't seem to get it to work through any shell idea either. Maybe if I could find the process id of my previous app that could work but I still don't know what to do.
hi dukbert
u add the procedure named 'Main' in a module and make the startup of project as 'Sub Main' in project properties.
a sample proc should look like this....
Public Sub Main()
If App.PrevInstance Then
MsgBox "prev instance exist"
AppActivate "anand"
End
Else
Form1.Show ' u startup object
End If
End Sub
one thing u pls note , here "anand" means the title of ur application, i m doubfule 4 app.title. u make some hard coded title 4 ur application.
hope this works
bye anand
u add the procedure named 'Main' in a module and make the startup of project as 'Sub Main' in project properties.
a sample proc should look like this....
Public Sub Main()
If App.PrevInstance Then
MsgBox "prev instance exist"
AppActivate "anand"
End
Else
Form1.Show ' u startup object
End If
End Sub
one thing u pls note , here "anand" means the title of ur application, i m doubfule 4 app.title. u make some hard coded title 4 ur application.
hope this works
bye anand
be sure that the handle (lngHandle) returned from FindWindow is not 0. If it is 0, this means that the title you pass to the function is not correct.
ASKER
Still had a few problems with this answer, and for some reason it wouldn't work when I first tried it out. Anyway, I added something else and a few versions later it magically worked with this method. So sorry for taking so long to award the points
Option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Const SW_SHOW = 5
Public Sub Main()
Dim strTemp As String
Dim lngHandle As Long
If App.PrevInstance Then
MsgBox "Application déjà loadée!!!"
lngHandle = FindWindow(strTemp, "Test PrevInstance")
Call ShowWindow(lngHandle, SW_SHOW)
Call SetFocus(lngHandle)
Call SetActiveWindow(lngHandle)
Call SetForegroundWindow(lngHan
End
Else
frmPrevInstance.Show
End If
End Sub