+ Reply to Thread
Results 1 to 13 of 13

Loop Through Active Windows and Activate

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Loop Through Active Windows and Activate

    Hi,

    Can anyone help me with some code to loop through all my open window captions and if one matches "Session" then activate it and Bring the window into focus.

    I tried AppActivate with the name "Session A" Which sort of works but only if the user has the name "Session A". I found some users have Session B Open instead and any other letter as well the only thing in common is "Session"

    I also found something else odd that it appears at times to Activate the window but wont Bring the window into Focus or show the window.

    Can anyone offer any suggestions?

    Thanks, Mike
    Last edited by realniceguy5000; 11-02-2011 at 07:23 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Loop Through Active Windows and Activate

    if you mean a workbook by the word Window then

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Loop Through Active Windows and Activate

    Thanks,

    No I mean the applications in windows.

    I'm trying to switch from Excel workbook to another application with the windows caption being "Session" as a common name.

    Mike

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Loop Through Active Windows and Activate

    I assume you mean the window caption containing 'session' rather than being 'session'?
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Loop Through Active Windows and Activate

    Yes,
    What I'm trying to do is activate and setfocus (show the window).
    The application i'm trying to move to is an AS400 Application. With a windows caption name of "Session A" But would like the script to find any window caption that contains "Session"

    Do you have any ideas?

    Thank You, Mike

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop Through Active Windows and Activate

    I just gave that code yesterday in another thread:

    Please Login or Register  to view this content.



  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Loop Through Active Windows and Activate

    I'd probably use WinAPI for that (don't have code to hand as I'm on a Mac) but you could adapt snb's code to do what you need, assuming you can loop through the tasks collection and check the name.

  8. #8
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Loop Through Active Windows and Activate

    I found these scripts below, but it cant find the window for some reason? Maybe once again I'm doing something wrong?


    Still working with SNB script as well but not working either at this time.

    Seems like a lot of code compared to snb method???

    Thank You, Mike

    Here is my code for calling the functions

    Please Login or Register  to view this content.
    Here is one function I have in a mod
    Please Login or Register  to view this content.
    Here is the other function I have in another mod.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Loop Through Active Windows and Activate

    I don't know if you've noticed, but you're unlikely to ever find shorter code than snb posts, if that's your main criterion.

    Can't test at the moment, but are you sure you have the case of your window title correct? Like is case sensitive.

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop Through Active Windows and Activate

    to get an overview:

    Please Login or Register  to view this content.
    You could try different things to get what you want (I can't see how 'unactive' this session is)

    Please Login or Register  to view this content.
    Last edited by snb; 11-02-2011 at 06:26 AM.

  11. #11
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Loop Through Active Windows and Activate

    Once Again snb leads the way with the shortest working code.

    @ romperstomper
    You were right after running the first part of snb code I got a list of windows. Session A had this added onto the end -[24x80] so maybe that is why it wasn't working correctly, not sure.

    @ snb

    After playing around with your last code, I was able to get this below to work. I only removed the second with statement as it crashed on those lines. When you first posted your code I was confused (like normal) by the Word.Application part. I thought it was trying to activate a MS Word document which I wasn't working with.

    But anyway here is what I'm using and it appears to be working just fine.

    Please Login or Register  to view this content.
    Thank You So Much for the help !!!! Again...

    Mike
    Thank You, Mike

    Some Helpful Hints:

    1. New members please read & follow the Forum Rules
    2. Use Code Tags...Place[code]Before the first line and[/code] After the last line.
    3. If you are pleased with a solution mark your post SOLVED.
    4. Thank those who have help you by clicking the scales at the top right of the post.

    Here...

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Loop Through Active Windows and Activate

    FWIW, an API version would be something like this:
    Please Login or Register  to view this content.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop Through Active Windows and Activate

    As you may be well aware of, the development teams of MS do not communicate too often.
    So it happens that some 'system' properties (like open applications) or the screenproperties (horizontally & vertically) have been built into the Word-VBA-library and not into the Excel VBA library.

    For who is interested:

    Please Login or Register  to view this content.
    It's a simple method to adapt e.g. a userform to the user's screen screenresolution.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1