Connecting Tech Pros Worldwide Forums | Help | Site Map

VB with Shell

Member
 
Join Date: Mar 2007
Posts: 49
#1: Aug 16 '07
Hi to everybody,

How to open the files otherthan .exe by using VB

can any body tell with example please

Thank u

Expert
 
Join Date: Aug 2007
Posts: 122
#2: Aug 16 '07

re: VB with Shell


In your declarations at the top of the code, paste:

Expand|Select|Wrap|Line Numbers
  1. Public 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
And then use this function:

Expand|Select|Wrap|Line Numbers
  1. Public Function ShellExec(ByVal strFile As String)
  2.     Dim x As Long: x = ShellExecute(0, "OPEN", strFile, "", "", 0)
  3. End Function
Passing the filepath as a parameter.

Hope that helps.
Expert
 
Join Date: Aug 2007
Posts: 122
#3: Aug 16 '07

re: VB with Shell


And the example you requested:

Private Sub cmdOpen_Click()
ShellExec("C:\testfile.doc")
End Sub
pureenhanoi's Avatar
Familiar Sight
 
Join Date: Mar 2007
Location: VietNam
Posts: 175
#4: Aug 17 '07

re: VB with Shell


Quote:

Originally Posted by Stwange

In your declarations at the top of the code, paste:

Expand|Select|Wrap|Line Numbers
  1. Public 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
And then use this function:

Expand|Select|Wrap|Line Numbers
  1. Public Function ShellExec(ByVal strFile As String)
  2.     Dim x As Long: x = ShellExecute(0, "OPEN", strFile, "", "", 0)
  3. End Function
Passing the filepath as a parameter.

Hope that helps.

Woa, that's very nice. Sometimes I would be called Shell excute function, but Shell command in VB can't archive parameters with space character.
Reply