Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic Classic (6 and earlier)
Expertise: Advanced
Jun 30, 2009

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 Long

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

lRet = ShellExecute(Application.hwnd, "Open", _
   "<Full path and filename here>", vbNullString, _
   "<Working directory here>", 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.

Pavana Shireesha
Thanks for your registration, follow us on our social networks to keep up-to-date