Set focus to application opened when using ShellExecute

Posted on 2008-11-14
Last Modified: 2012-06-22
I use the following line of code as part of a public sub routine to open various types of files from within Access:
 iRet = ShellExecute(Forms("frmActivity").hWnd, "open", sDocFileNamePath, "", "", nCmdShow)

The code will open any file where the extension has been associated with a program, but the problem is as soon as the file opens, the form goes back to Access instead of the file that's been opened.  nCmdShow is a numeric value that is supposed to set whether the program is opened minimized, maximized, etc, but it doesn't matter what numeric value I use here, the application still minimizes after opening.
Question by:Mach1pro
    LVL 44

    Accepted Solution

    Add the line:

    AppActivate iRet
    LVL 44

    Expert Comment

    Type Alt+F11 to get to the VBA window, and enter AppActivate in the Help window.  Also read up on Shell
    LVL 6

    Author Closing Comment

    That seems to work for most applications. Tends to throw up an error when activating Excel. Since Excel isn't going to be launched that much, I just trapped the error and went with it.
    LVL 44

    Expert Comment

    Thanks, glad to help.  No reason I can think of why it should not work for Excel.  Go to the VBA window and in the immediate pane repeat the code you used to Open Excel.  Here you execute one line at a time.  Make sure you have the path for both Excel, and the spreadsheet down correctly, and don't forget the double quotes.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now