Open Any File from Code

Open Any File from Code

Using the following code snippet you can run any application (such as Word, Excel, etc.) or open any file whose extension is associated with an application, such as .doc, .txt, or other data files.

First, declare this Windows API function at module level:

Private Declare Function ShellExecute Lib _   "shell32.dll" Alias "ShellExecuteA" ( _   ByVal hwnd As Long, ByVal lpOperation As String, _   ByVal lpFile As String, ByVal lpParameters As String, _   ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongConst SW_SHOWNORMAL = 1

Then, in your code, add the following line when you need to open a file:

lRet = ShellExecute(Application.hwnd, "Open", _   "", vbNullString, _   "", SW_SHOWNORMAL)

Substitute the path and filename of the file you want to run or open and the working directory for the target application for the portions of the preceding line shown in angle brackets.

See also  5 Tips for Working With an Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist