Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 64,574 times

Related Categories

Open a file/url with its default program

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:support@vbweb.co.uk", vbNullString, vbNullString, vbNormalFocus
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [278] Open a file/url with its default program

    Posted by Naki on 06 Oct 2007

    Here's how it's done in VB 2005:


    System.Diagnostics.Process.Start("www.mysite.com")

  • Test this

    Posted by crazybanana on 20 Dec 2002

    Seems that the problem is to get vba to accept """"c:test.doc""""

    Test to set

    openfile = "c:test.doc"
    'And change """"c:test.doc"""" to openfile
    ShellExecute 0, vbNullString, openfile, vbNullS...

  • Got some cash?

    Posted by sasa on 21 Oct 2002

    Apparently this software does it. AccessToVb http://www.gfminc.com/AccessToVBPFS.htm

    Sasa

  • MS Access with VB

    Posted by Fletcher on 03 Apr 2002

    :confused: I have made a program in MS Access and now want to change to VB but without having to redesign all the Forms. Is there a way that this can be done. I would be very grateful for any type of...

  • Posted by void on 03 Apr 2002

    ...Or, without the '(' & ')'.

    ShellExecute 0, vbNullString, """"C:\test.doc"""", vbNullString, vbNullString, vbNormalFocus