Results 1 to 2 of 2

Thread: How do I use SHELL to open app only once??

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2001
    Posts
    42

    Question How do I use SHELL to open app only once??

    I need to use shell to open an application only if the application is not already open.

    If the user tries to open it when it is open it should be ignored whereas right now it'll open another window with the application.

    Anyone know how??

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Something like this. You must know at least part of the window caption, which shouldn't be a problem because things like "Microsoft Word", "Microsoft Internet Explorer","Notepad" usually get appended to the end

    In a Module
    VB Code:
    1. Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    3. Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
    4.  
    5. Private Const WM_GETTEXT = &HD
    6.  
    7. Private aCaptions() As String
    8. Private lCount As Long
    9.  
    10. Public Function GetAllCaptions() As Variant
    11.     lCount = 0
    12.     Call EnumWindows(AddressOf EnumWindowsProc, 0&)
    13.     If lCount Then ReDim Preserve aCaptions(lCount - 1)
    14.     GetAllCaptions = IIf(lCount, aCaptions, Array())
    15. End Function
    16.  
    17. Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    18.     Dim sBuffer As String * 260
    19.     If IsWindowVisible(hwnd) Then
    20.         ReDim Preserve aCaptions(lCount)
    21.         aCaptions(lCount) = Left(sBuffer, SendMessage(hwnd, WM_GETTEXT, 260, ByVal sBuffer))
    22.         If Len(Trim(aCaptions(lCount))) Then lCount = lCount + 1
    23.     End If
    24.     EnumWindowsProc = hwnd
    25. End Function
    Form Code:
    VB Code:
    1. Public Function IsOpen(strTitle As String) As Boolean
    2.    
    3.     Dim vCaps As Variant
    4.     Dim lIndex As Long
    5.    
    6.     vCaps = GetAllCaptions()
    7.  
    8.     For lIndex = 0 To UBound(vCaps)
    9.         If InStr(1, vCaps(lIndex), strTitle, vbTextCompare) Then
    10.             IsOpen = True
    11.             Exit Function
    12.         End If
    13.     Next
    14.    
    15.     IsOpen = False
    16.  
    17. End Function
    18.  
    19. Private Sub Command1_Click()
    20.  
    21. If IsOpen("Notepad") = False Then
    22.     Shell "Notepad.exe", vbMaximizedFocus
    23. End If
    24.  
    25. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width