A common VB question is how to open a file (ie a Word document) with its default application when you don't know where the application is installed? The answer is... the same way Explorer does, using the ShellExecute API function. This also lets you open a web address (starting with http://) or an email address (starting with mailto:). The code below gives you an example
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
'// open file (quotes are used so that the actual value that is passed is "C:\test.doc"
Private Sub cmdOpen_Click()
ShellExecute 0, vbNullString, """"C:\test.doc"""", vbNullString, vbNullString, vbNormalFocus
End Sub
'// open url
Private Sub cmdOpen_Click()
ShellExecute 0, vbNullString, "http://www.vbweb.co.uk/", vbNullString, vbNullString, vbNormalFocus
End Sub
'// open email address
Private Sub cmdOpen_Click()
ShellExecute 0, vbNullString, "mailto:[email protected]", vbNullString, vbNullString, vbNormalFocus
End Sub
Comments