Set focus to application opened when using ShellExecute

Posted on 2008-11-14
Medium Priority
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
  • 3
LVL 44

Accepted Solution

GRayL earned 1200 total points
ID: 22960334
Add the line:

AppActivate iRet
LVL 44

Expert Comment

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

Author Closing Comment

ID: 31516779
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

ID: 22960891
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 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