Results 1 to 4 of 4

Thread: Opening a file with its app

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    3
    I want to be able to open a file from within my VB app, based on the file's extension (e.g. if the file is file.doc,the file would open in Word). This is basically the same behavior you get if you click on a file who has his extension registered in the file types.

    Shell and CreateProcess both require the app name (in my example above, I'd have to use 'word.exe file.doc'). I'd like a call that would simply tell Windows to open the file with the app that it thinks should be used with that file.

    Any ideas? Thanks.

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Use the ShellExecute API, which uses the associated application to open a file.

    http://www.vbapi.com/ref/s/shellexecute.html

    ~seaweed

  3. #3
    New Member
    Join Date
    Jun 2000
    Posts
    2

    Smile

    'Yes, you need to use the ShellExecute API.

    'The declaration is:

    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

    'Constants which are used to determine the state of the app when it loads...

    Private Const SW_SHOWNORMAL = 1
    Private Const SW_SHOWMINIMIZED = 2
    Private Const SW_SHOWMAXIMIZED = 3
    Private Const SW_SHOWNOACTIVATE = 4
    Private Const SW_SHOWMINNOACTIVE = 7
    Private Const SW_SHOWNA = 8
    Private Const SW_SHOWDEFAULT = 10

    'Then call it in code like this:

    ShellExecute hwnd, "open", "http://www.vb-world.net", "", "", SW_SHOWNORMAL

    'The last variable can be replaced with any of the constants above. It can be used to open websites with the default browser, and email messages with the current email program, or replace with a path and filename to open a document with its' associated application.

    'Enjoy.



  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    3
    Thanks, both JFrench and Seawood, your advice was exactly what I needed. I can't believe I overlooked ShellExecute before.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width