devxlogo

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.

devx-admin

Share the Post: