Results 1 to 7 of 7

Thread: Switching between applications

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Cool Switching between applications

    Can someone please tell me if it's possible, and if it is where I can find some sample VB code that would allow me, to programmatically switch between MS Word, MS Excel and the internet, and also pass data between them? Thanks.

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Do you mean like emulating a user doing Copy, Alt Tab, Paste?

    If so, this may help with the Alt Tab bit:

    Finding the last application used:
    That is, if the user is using an application with this code in it, what application would they switch to when they press ALT TAB.
    Note: Within Windows 98 it is not possible to use the VB SendKeys function to send an ALT TAB sequence. Therefore this code needs to be used in order to activate the previously used application.

    The code involves stepping through all of the open windows on the system, and identifying which windows:
    · Don’t have parents (top level of an application)
    · Have a title bar and caption (Windows can be identified and listed)
    · Are visible (not hidden from view)
    · Are not from this application.

    The module code required is:
    VB Code:
    1. Private Declare Function IsWindowVisible Lib "user32" _
    2. (ByVal hWnd As Long) As Long
    3. Public Declare Function EnumWindows Lib "user32" _
    4. (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    5. Public Declare Function GetWindowText Lib "user32" _
    6. (ByVal hWnd As Long, ByVal lpString As String, _
    7. ByVal cch As Long) As Long
    8. Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
    9. Public Declare Function GetWindowLong Lib "user32" _
    10. (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    11. Declare Function GetActiveWindow Lib "user32" () As Long
    12.  
    13. Public thisHwnd As Long
    14. Public FirstApp As Long
    15. Public FirstAppTitle As String
    16. Public Const GWL_STYLE = -16
    17. Public Const WS_CAPTION = &HC00000
    18. Public Const MAX_LEN = 260
    19.  
    20.  
    21. Public Function _
    22.  EnumWinProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
    23. Dim lRet, lWindowStyle, lWindowParent As Long
    24. Dim strBuffer As String
    25.    
    26. ' Only keep windows that are visible,
    27. ' don't have parents, have a title bar and a caption,
    28. ' and are not this application, and are the first application found
    29.     If hWnd <> thisHwnd Then                         ' Not this app
    30.         If IsWindowVisible(hWnd) Then                ' Window visible
    31.             lWindowParent = GetParent(hWnd)
    32.             If lWindowParent = 0 Then                ' No parents
    33.                 lWindowStyle = GetWindowLong(hWnd, GWL_STYLE)
    34.                 If (lWindowStyle And WS_CAPTION) Then ' Has a caption
    35.                     strBuffer = Space(MAX_LEN)
    36.                     lRet = GetWindowText(hWnd, strBuffer, _
    37.  Len(strBuffer))
    38.                         If lRet Then                  ' Has a title
    39.                             If FirstApp = 0 Then
    40.                                 FirstApp = hWnd
    41.                                 FirstAppTitle = strBuffer
    42.                             End If
    43.                         End If
    44.                 End If
    45.             End If
    46.         End If
    47.     End If
    48.     EnumWinProc = 1
    49. End Function
    This routine is called using:
    VB Code:
    1. thisHwnd = GetActiveWindow()
    2. FirstApp = 0
    3. Call EnumWindows(AddressOf EnumWinProc, 0)
    4. MsgBox "The last application we were using was " & FirstApp & _
    5.         ", and has a Title of " & FirstAppTitle
    The return from this routine checks for the Class name of the application, and looks for the following in particular (any others are reported as “Not useful”):
    VB Code:
    1. Select Case LastAppClass
    2.         Case "OpusApp"
    3.             WhoCalledUs = "Word"
    4.         Case "XLMAIN"
    5.             WhoCalledUs = "Excel"
    6.         Case "rctrl_renwnd32"
    7.             WhoCalledUs = "Outlook"
    8.         Case "ExploreWClass"
    9.             WhoCalledUs = "WinExp"
    10.         Case "PP9FrameClass"
    11.             WhoCalledUs = "PPT"
    The following line will activate the previous application:
    AppActivate FirstAppTitle

    All the above code either works or it does not. If it does, then its likely that I "borrowed" it from elsewhere on this board. If it doesn't work, then its likely to be mine.


    PLUS
    The cut and paste bit can be done using the Clipboard functions.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Thanks for info - and ..

    yes, I am talking about moving between applications. But I'm most interested in being able to execute more VB code afeter I've switched to the new app. For instance, while executing code on an Excel WB, I'd like to switch to follow a hyperlink, retrieve some data (by executing somemore VB code), switch back to Excel, apply the retireved data, the switch to Word, execute somemore VB code, switch back to Excel, etc.

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    The previous code can help to identify which application you want to be active.

    The AppActivate command will activate it for you. Once it is active, your code is no longer active.

    However, what you can do is rather than activating the application, you can (as long as it has a callable interface like Word, Excel, Outlook etc.) send it commands....

    E.g.:
    VB Code:
    1. Set Obj = Word.ActiveDocument
    2.     If Obj.Saved = False Then Obj.Save
    3.     OrigFullFile = Obj.FullName
    4.     tempFile = Environ("Temp") & "\Document.DOC"
    5.     Obj.SaveAs tempFile
    6.     Obj.SaveAs OrigFullFile
    which takes a copy of the current document, and saves it to the temporary folder as Document.DOC

    For Excel:
    VB Code:
    1. Set App = GetObject(, "Excel.Application")
    2.     Set Obj = App.ActiveWorkbook
    3.     OrigFullFile = Obj.FullName
    For PowerPoint:
    VB Code:
    1. Set App = GetObject(, "PowerPoint.Application")
    2.     With App.ActivePresentation
    3.         OrigFullFile = .FullName
    4.         tempFile = Environ("Temp") & "\PowerPoint.PPT"
    5.         .SaveCopyAs tempFile
    For Outlook 2000:
    VB Code:
    1. Set olExp = Outlook.ActiveExplorer
    2.     Set olCurrentFolder = olExp.CurrentFolder
    3.     Set olFolderItems = olCurrentFolder.Items
    4.     Set myOlSel = olExp.Selection
    5.     For x = 1 To myOlSel.Count
    6.         On Error Resume Next
    7.         gSubject = myOlSel.Item(x).Subject

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Thanks.

    Thanks a lot. I'll give it a try.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382
    Hello. I still cannot get this to work. when I execute, I get error message from Excel:
    "Run-time error 453.
    Can't find DLL entyr point GetWindowLong in user32"

    What gives? Thanks.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Switching between applications (resolved)

    I looked at other examples posted here and found the use of ALIAS. Added Alias "GetWindowLongA" and Alias "GetWindowTextA" to my function declares and that fixed the problem. Thanks.

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